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.
Now, the if statement returns 1, as it should. This is expected. However, what’s unexpected happens after I set $node: after setting it, it appears to be FALSE. What? Just a few lines before, my fetch() attempt returned the expected value, so I have no idea what’s happening.
$sth = $dbh->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->bindParam(':user_flow', $user->info->flow);
$sth->bindParam(':input', $input_sentence);
$sth->execute();
// If node exists.
if ($sth->fetch()->num_rows > 0)
{
// Get the information for the node.
$node = $sth->fetch();
[...] etc
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 $sth->fetch()->num_rows, 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.
Thanks! 🙂
EDIT:
Notice: Undefined variable: node_count ... on line 56
// Retrieve all child nodes under $parent.
$node_query = $dbh->prepare("
SELECT *, COUNT(*) AS num_rows
FROM flow
WHERE parent = :parent
");
$node_query->bindParam(':parent', $parent);
$node_query->execute();
// If child nodes exist.
if ($node_count = $node_query->fetch() && $node_count->num_rows > 0) // Line 56
{
[...] etc
Assign the data array to some variable and use it without further fetches: