Note that there are some explanatory texts on larger screens.

plurals
  1. POIs is possible to use the same variables and checks in multiple prepared statements in PDO?
    primarykey
    data
    text
    <p>I am still trying to convert mysql_* things into PDO and prepared statements. It is really hard work to do this in entire web but I am not giving up and I ran into some problems.</p> <p>My question is about multiple <code>mysql_query()</code> commands in one function. So when i have something like this in code:</p> <pre><code>if (something) { mysql_query("UPDATE account SET pass=$pass WHERE id=$id"); mysql_query("UPDATE account_2 SET lock=$lock WHERE id=$id"); mysql_query("UPDATE account_3 SET surname=$surname WHERE id=$id"); } </code></pre> <p>And want to tranfer it to PDO an prepared statements. I already know I have to do something like this:</p> <pre><code>if (something) { $stmt = $db-&gt;prepare("UPDATE account SET pass=:pass WHERE id=:id"); $stmt-&gt;bindValue(':pass', $pass, PDO::PARAM_STR); $stmt-&gt;bindValue(':id', $id, PDO::PARAM_INT); $stmt-&gt;execute(); } </code></pre> <p>I have actually two questions: Is it ok to bindValue password as string (PDO::PARAM_STR)? When I add other statements as it has more queries should I name them differently like $stmt2 or when it is executed I can do only $stmt all the time like this?</p> <pre><code>if (something) { $stmt = $db-&gt;prepare("UPDATE account SET pass=:pass WHERE id=:id"); $stmt-&gt;bindValue(':pass', $pass, PDO::PARAM_STR); $stmt-&gt;bindValue(':id', $id, PDO::PARAM_INT); $stmt-&gt;execute(); $stmt = $db-&gt;prepare("UPDATE account_2 SET lock=:lock WHERE id=:id"); $stmt-&gt;bindValue(':lock', $lock, PDO::PARAM_INT); $stmt-&gt;bindValue(':id', $id, PDO::PARAM_INT); $stmt-&gt;execute(); } </code></pre> <p>Or should I rather do this?</p> <pre><code>if (something) { $stmt = $db-&gt;prepare("UPDATE account SET pass=:pass WHERE id=:id"); $stmt-&gt;bindValue(':pass', $pass, PDO::PARAM_STR); $stmt-&gt;bindValue(':id', $id, PDO::PARAM_INT); $stmt-&gt;execute(); $stmt2 = $db-&gt;prepare("UPDATE account_2 SET lock=:lock WHERE id=:id"); $stmt2-&gt;bindValue(':lock', $lock, PDO::PARAM_INT); $stmt2-&gt;bindValue(':id', $id, PDO::PARAM_INT); $stmt2-&gt;execute(); } </code></pre> <p>And one more at the end. When I have in code function/check like this:</p> <pre><code>if (mysql_query("INSERT INTO account (id, pass, email, request_time, status) VALUES ('".$id."', '".$pass."', '".$mail."', '".time()."', '".$status."')")) { blabla } </code></pre> <p>How to use suck check in stmt and PDO if the insert command was executed if it needs to be there in PDO in the first place? </p> <p>And last question ... when I already bindValue in prepared statement in PDO do I later in code still need to use is_numeric() function?</p> <p>So Summary:</p> <p>1) Is it ok to bindValue password as string (PDO::PARAM_STR)?</p> <p>2) When I add statements as it has more queries should I name them differently like $stmt, $stmt2, $stmt3 or every statement the same?</p> <p>3) When I already bindValue in prepared statement in PDO do I later in code still need to use is_numeric() function (maybe for some cheating with the variable)?</p> <p>Thank you all</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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