Run 2 queries at the same time is it possible?

1

I need to change all the fields in a table and insert only one row in another 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.

    
asked by anonymous 20.02.2016 / 00:55

1 answer

1

This behavior is expected, the second query overrides the value of $stmt , you can solve this in two ways, create two prepared statemetns, or double-call excute() after 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();  
    
20.02.2016 / 01:37