0

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...

4
  • What is the Problem with your code? Commented Aug 22, 2017 at 9:26
  • What is the error that you are getting? Commented Aug 22, 2017 at 9:28
  • 2
    So you just want to update all records that have status=1 to have status=2? Then you don’t need to select them first and then go over them in a loop ... all you need is one single statement, UPDATE table SET status=2 WHERE status=1, and done ... Commented Aug 22, 2017 at 9:28
  • I want to select each row to assign variables and do stuff.. this why I use foreach but I also want to update each row when done doing stuff.. Commented Aug 22, 2017 at 9:45

5 Answers 5

1

You could just all do it in one query really:

UPDATE table SET status=2 WHERE status=1

Is there a reason why you want to do this one by one? If it is performance reasons, you can limit your query in batches like this:

UPDATE table SET status=2 WHERE status=1 LIMIT 100

If you really need to loop them one by one, putting this snippet in your if statement should help a bit:

$updateSql = "UPDATE table SET status=2 WHERE id=:id";

// Prepare statement
$stmt = $db->prepare($updateSql);
$stmt->bindParam(":id", $id); 

// execute the query
$stmt->execute();

Still, you should make your select more efficient by limiting the query to rows with status = 1 and limiting it to the data you actually need, so you don't need to do the if and just run my snippet in the loop directly:

SELECT id FROM table WHERE status = 1
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks but, yes, I want to get the data from each row and run some code, then update the record and run the code for the next row...
added more info in post to run the update statement on a one by one basis in your php loop.
0

Your query can be simplified to:

UPDATE `table` SET status=2 WHERE status=1

In php:

$sql = 'UPDATE `table` SET status=2 WHERE status=1';
$db->query($sql);

Comments

0

I want to update each selected record with a status of 1 to 2...

If you want to update records you don't need to make the select statement first. In fact, it doesn't work this way , because you're updating single values in your script without any impact or whatsoever on your database

as a notice you may consider to add

$db->setAttribute(PDO::ATTR_EMULATE_PREPARE,false)

to make the prepared statement activated, as a matter of fact this is a security mesure

Comments

0

As I good understand youcan do it this way...

 $sth = $db->prepare("INSERT INTO table VALUES(:value, :value1, :value2, ...)");

foreach ($db->query($sql) as $row) {

 $id = $row['id'];
 $status= $row['status'];


 if($status==1) {

 $sth->bindParam(':value',$someValue));
 $sth->bindParam(':value1',$someValue1));
 $sth->bindParam(':value2',$someValue2));
 //...
 $sth->execute();

 }

Comments

0

If i understand you correctly, just use the query

UPDATE table SET status=2 WHERE id = :id

and bind :id to the extracted $id using PDO prepare

Also your SELECT query should be like this:

SELECT id, status FROM table WHERE status = 1

If you don't need to use the data returned by the SELECT statement just hit an UPDATE query to your database without selecting like this:

UPDATE table SET status=2 WHERE status=1

However, if there are many records to be updated (talking about 100k+), this wouldn't be the best choice.

Please note that table should have an index on status, because the query shouldn't need to scan every row, but know which rows to update.

4 Comments

why should the table has a index ?
I know what index is but you should not use index to use index. USing index could also slower your query.
W-What? "not use index to use index"?! What do you mean?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.