Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<ol> <li>I would use diferent queries for sure, so you can have control on what you are doing. </li> <li><p>I would use a transaction. If we read <a href="http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers#Transactions" rel="nofollow">this simple explanations</a>, pdo transactions are quite straightforward. They give us this simple but complete example, that ilustrates how everithing is as we should expect (consider <code>$db</code> to be your <code>DB::connection()-&gt;pdo</code>). </p> <pre><code>try { $db-&gt;beginTransaction(); $db-&gt;exec("SOME QUERY"); $stmt = $db-&gt;prepare("SOME OTHER QUERY?"); $stmt-&gt;execute(array($value)); $stmt = $db-&gt;prepare("YET ANOTHER QUERY??"); $stmt-&gt;execute(array($value2, $value3)); $db-&gt;commit(); } catch(PDOException $ex) { //Something went wrong rollback! $db-&gt;rollBack(); echo $ex-&gt;getMessage(); } </code></pre></li> <li><p>Lets go to your real statements. For the first of them, the <code>SELECT ...</code>, i wouldn't use <code>exec</code>, but <code>query</code>, since as stated <a href="http://www.php.net/manual/en/pdo.exec.php" rel="nofollow">here</a> </p> <blockquote> <p>PDO::exec() does not return results from a SELECT statement. For a SELECT statement that you only need to issue once during your program, consider issuing PDO::query(). For a statement that you need to issue multiple times, prepare a PDOStatement object with PDO::prepare() and issue the statement with PDOStatement::execute().</p> </blockquote> <p>And assign its result to some temp variable like</p> <p><code>$result= $db-&gt;query ($select);</code></p></li> <li><p>After this execution, i would call <code>$result-&gt;fetchAll()</code>, or <code>$result-&gt;closeCursor()</code>, since as we can read <a href="http://www.php.net/manual/en/pdo.query.php" rel="nofollow">here</a></p> <blockquote> <p>If you do not fetch all of the data in a result set before issuing your next call to PDO::query(), your call may fail. Call PDOStatement::closeCursor() to release the database resources associated with the PDOStatement object before issuing your next call to PDO::query().</p> </blockquote></li> <li><p>Then you can <code>exec</code> the update</p> <pre><code>$result= $db-&gt;exec($update); </code></pre></li> <li><p>And after all, just in case, i would call again <code>$result-&gt;fetchAll()</code>, or <code>$result-&gt;closeCursor()</code>.</p></li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload