Speed up development with full-stack environments for every branch.

Learn More

Update and delete rows [sqlite3 and PHP]

248 Runs 4860 Views 1251 Copies
Saved

Saved

gtsolutions 373

gtsolutions
published 4 years ago

<pre>
<?php

/*
 * PHP SQLite3 - Update and delete rows
 */
//a file that creates and populates the table used in this example
require_once('createmyusers.php');

//Open the database mydb
$db = new SQLite3('db/mydb');

//Get all rows from myusers table 
echo "Get all rows from myusers table before delete<br />";
$results = $db->query('SELECT username,password FROM myusers');
while ($row = $results->fetchArray()) {
    var_dump($row);
}


echo "admin user before the update<br />";
$admin = $db->querySingle('SELECT username,password FROM myusers WHERE username="admin"', true);
var_dump($admin);

//Update the record
$db->exec('UPDATE myusers SET password="anotherpass" WHERE username="admin"');

echo "admin user after the update (password has been changed)<br />";
$admin = $db->querySingle('SELECT username,password FROM myusers WHERE username="admin"', true);
var_dump($admin);


//Get all rows from myusers table 
echo "Get all rows from myusers table before delete<br />";
$results = $db->query('SELECT username,password FROM myusers');
while ($row = $results->fetchArray()) {
    var_dump($row);
}

//Delete a record
$db->exec('DELETE FROM myusers WHERE username="admin"');
echo "User admin deleted <br />";


//Get all rows from myusers table 
echo "Get all rows from myusers table after delete<br />";
$results = $db->query('SELECT username,password FROM myusers');
while ($row = $results->fetchArray()) {
    var_dump($row);
}
?>
Please login/signup to get access to the terminal.

Your session has timed out.

Dismiss (the page may not function properly).