以下是我的应用程序的代码片段。请记住,我对PDO非常非常陌生(就像今天开始弄清楚一样),所以我有点困惑。
现在,if
语句返回 1,这是应该的。这是意料之中的。但是,在我设置$node
后发生了意外:设置后,它似乎是 FALSE。什么?就在几行之前,我的fetch()
尝试返回了预期值,所以我不知道发生了什么。
$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
我猜光标向前移动,然后没有什么可读取的,所以返回 FALSE。不过,肯定有一种方法可以解决这个问题!当我运行$sth->fetch()->num_rows
时,我并没有试图改变任何东西——我只是试图读取一个值。有解决方法吗?我在做什么奇怪的事情吗?我太迷茫了,哈哈。
谢谢! :)
编辑:
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
将数据
数组分配给某个变量并使用它而无需进一步获取:
if (($row = $sth->fetch()) && $row->num_rows > 0) {
// work with $row here
}