title: MySQL PDO - $sth->fetch() returns FALSE, but only the second time?
body: <p>The following is a code snippet of my application. Bear in mind that I am very, very new to PDO (as in, started figuring it out today) so I'm a bit confused.</p>
  
  <p>Now, the <code>if</code> statement returns 1, as it should. This is expected. However, what's unexpected happens after I set <code>$node</code>: after setting it, it appears to be FALSE. What? Just a few lines before, my <code>fetch()</code> attempt returned the expected value, so I have no idea what's happening.</p>
  
  <pre><code>$sth = $dbh-&gt;prepare("
      SELECT *, COUNT(*) AS num_rows
      FROM flow
      INNER JOIN flow_strings
          USING(node_id)
      WHERE
          (
              parent = 0
              OR parent = :user_flow
          )
          AND source = 0
          AND string = :input
      ");
  $sth-&gt;bindParam(':user_flow', $user-&gt;info-&gt;flow);
  $sth-&gt;bindParam(':input', $input_sentence);
  $sth-&gt;execute();
  
  // If node exists.
  if ($sth-&gt;fetch()-&gt;num_rows &gt; 0)
  {
      // Get the information for the node.
      $node = $sth-&gt;fetch();
  
  [...] etc
  </code></pre>
  
  <p>I am guessing that the cursor is moved ahead, and then there's nothing left to read, so FALSE is returned. Surely there's a way to work around this, though! When I run <code>$sth-&gt;fetch()-&gt;num_rows</code>, I'm not trying to change anyything--I'm just trying to read a value. Is there a workaround? Am I doing something weird? I'm so lost, haha.</p>
  
  <p>Thanks! :)</p>
  
  <hr>
  
  <h1>EDIT:</h1>
  
  <p><code>Notice: Undefined variable: node_count ... on line 56</code></p>
  
  <pre><code>// Retrieve all child nodes under $parent.
  $node_query = $dbh-&gt;prepare("
      SELECT *, COUNT(*) AS num_rows
      FROM flow
      WHERE parent = :parent
      ");
  $node_query-&gt;bindParam(':parent', $parent);
  $node_query-&gt;execute();
  
  // If child nodes exist.
  if ($node_count = $node_query-&gt;fetch() &amp;&amp; $node_count-&gt;num_rows &gt; 0) // Line 56
  {
  
  [...] etc
  </code></pre>