当$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的新手,但我似乎无法看到这里的错误。
提前致谢。
答案 0 :(得分:3)
显然,在使用预准备语句时,您不需要在SQL代码中将参数包装在引号中。
因此,准备语句应如下所示:
$query = $db->prepare(
"SELECT `season` " .
"FROM `tv` " .
"WHERE `show_url` = :show " .
"GROUP BY `season` " .
"ORDER BY `season` ASC;"
);