Note that there are some explanatory texts on larger screens.

plurals
  1. POStrange PDO select statement behaviour - selecting only first record
    primarykey
    data
    text
    <p><strong>EDIT: my bad it was a typo in db - all records had an extra whitespace at the beginning of code, apart from the first one - that's why I was getting only the first record</strong></p> <p>Its the simplest query - I find record with the corresponding <code>code</code> and <code>active</code> field with value 1. If there is such record I update its <code>active</code> field to 0.</p> <pre><code> # MySQL with PDO_MYSQL $DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass); $DBH-&gt;setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING ); $sql = "SELECT * FROM codes WHERE code=? AND active=1"; $STH = $DBH-&gt;prepare($sql); $STH-&gt;setFetchMode(PDO::FETCH_ASSOC); $result = $STH-&gt;execute(array($_POST["code"])); $row = $STH-&gt;fetchAll(); echo "&lt;pre&gt;", var_dump($row), "&lt;/pre&gt;"; echo $result , " rows: " . $STH-&gt;rowCount(); if($STH-&gt;rowCount() == 0){ echo "Error - this code is inactive"; }else{ echo "Code succesfully activated"; $STH = $DBH-&gt;prepare("UPDATE codes SET active=0 WHERE code=?"); $STH-&gt;execute(array($_POST["code"])); } </code></pre> <p>But it only works for the first record <code>847BKX</code>. If I try to search for others it always returns 0 rows even though the <code>active</code> field is still 1. Here is the snapshot from DB - <code>code</code> field is indexed <code>varchar</code> length 255. How come it only works for the first record and not others...?</p> <p><img src="https://i.stack.imgur.com/lNEas.png" alt="enter image description here"></p>
    singulars
    1. This table or related slice is empty.
    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