I want to update MySQL database records in a row...
The code below is used to connect the database with PDO and select a table, use foreach
loop to extract data from it and then, later I want to be able to update the status of each row based on the extracted data..
How this should be done correctly with PHP and MySQL, so that rows get extracted and updated one by one...
Code:
// Database connection
define('DBHOST','localhost');
define('DBUSER','username');
define('DBPASS','password');
define('DBNAME','database');
try {
//create PDO connection
$db = new PDO("mysql:host=".DBHOST.";dbname=".DBNAME, DBUSER, DBPASS);
$db->exec("set names utf8");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
//show error
echo '<p>'.$e->getMessage().'</p>';
exit;
}
$sql = 'SELECT * FROM table';
foreach ($db->query($sql) as $row) {
$id = $row['id'];
$status= $row['status'];
echo $status;
if($status==1) {
//Update status
//$sql_update = "UPDATE table SET status=2 WHERE id=". $id ."";
}
}
I want to update each selected record with a status of 1 to 2...