Running 2 queries at once is possible?

Asked

Viewed 1,005 times

1

I need to change all fields of a table and only insert another result into a row. I am using the following code

 $Nid = intval($_GET['Nid']);

$sql = "UPDATE * FROM programacao SET status='offline' WHERE status ='online' ";
$sql2 = "UPDATE programacao SET status='online' WHERE Nid = :Nid";
try {

  $stmt = $DB->prepare($sql);
  $stmt = $DB->prepare($sql2);
  $stmt->bindValue(":Nid", $Nid);

   $stmt->execute();  

The code is working but only executes the second query.

1 answer

1


This behavior is expected, the second query overwrites the value of $stmt, you can solve this in two ways, create two Prepared statemetns or call twice the excute() respectively after the prepare()

Option 1

$stmt1 = $DB->prepare($sql);  
$stmt2 = $DB->prepare($sql2);
$stmt2->bindValue(":Nid", $Nid);

$stmt1->execute();    
$stmt2->execute();    

Option 2

$stmt = $DB->prepare($sql);
$stmt->execute();    

$stmt->closeCursor();

$stmt = $DB->prepare($sql2);
$stmt->bindValue(":Nid", $Nid);
$stmt->execute();  
  • I did exactly option 2, it worked perfect worth

Browser other questions tagged

You are not signed in. Login or sign up in order to post.