PDO准备的声明未返回预期结果

时间:2012-06-17 15:05:48

标签: php mysql pdo prepared-statement

$this->show设置为saved-by-the-bell时,准备好的查询会返回以下结果集:

season
------
1
2
3
4

当我执行以下代码时:

$seasons = array( );

$query = $db->prepare(
  "SELECT `season` " .
  "FROM `tv` " .
  "WHERE `show_url` = ':show' " . 
  "GROUP BY `season` " .
  "ORDER BY `season` ASC;"
);

$query->bindParam( ':show', $this->show );
$query->execute( );
$query->setFetchMode( PDO::FETCH_OBJ );

while( $row = $query->fetch( ) )
{
  $season = new stdClass;
  $season->number = $row->season;
  $season->title = "Season {$row->season}";
  $season->url = $row->season;

  $seasons[] = $season;
}

return $seasons;

$seasons是一个空数组,为什么?

$ db是一个扩展PDO的实例化对象。

我尝试了各种调试方法,但是当我回显rowCount时,它说的是0.我还是PDO的新手,但我似乎无法看到这里的错误。

提前致谢。

1 个答案:

答案 0 :(得分:3)

啊,我明白了。我会把我的愚蠢留在这里,以防其他人发生这种情况。

显然,在使用预准备语句时,您不需要在SQL代码中将参数包装在引号中。

因此,准备语句应如下所示:

$query = $db->prepare(
  "SELECT `season` " .
  "FROM `tv` " .
  "WHERE `show_url` = :show " . 
  "GROUP BY `season` " .
  "ORDER BY `season` ASC;"
);