PDO MYSQL问题

时间:2010-12-16 03:57:58

标签: php mysql pdo

我在设置此功能时遇到了困难。可能是一个非常简单的修复,但我刚刚开始使用PDO,我不确定如何使这个工作。

我的问题在这里

for( $i = 1; $row = $STH->fetch(); $i++ ) {

每当代码执行时,都会到达

Fatal error: Call to a member function fetch() on a non-object in /usr/home/webmaster/nano/pages/search.php on line 21

非常感谢所有帮助

if($_POST['keyword']) {
$start_time = getmicrotime();
$keyword = addslashes( $_POST['keyword'] );
$results = addslashes( $_POST['results'] );
$STH = $DBH->query('SELECT p.page_url AS url,
                       COUNT(*) AS occurrences 
                       FROM search_page p, search_word w, search_occurrence o
                       WHERE p.page_id = o.page_id AND
                       w.word_id = o.word_id AND
                       w.word_word = "$keyword"
                       GROUP BY p.page_id
                       ORDER BY occurrences DESC
                       LIMIT $results');

$end_time = getmicrotime();

echo '<h2>Search results for '.$_POST['keyword'].':</h2>';
for( $i = 1; $row = $STH->fetch(); $i++ ) {
    echo '$i. <a href='.$row['url'].'>'.$row['url'].'</a>\n';
    echo '(occurrences: '.$row['occurrences'].')<br><br>\n';
}
print "query executed in ".(substr($end_time-$start_time,0,5))." seconds.";

1 个答案:

答案 0 :(得分:0)

您的SQL错误,因此您的$ STH不会获得结果对象,而是设置为false

SQL字符串需要'单引号。你把它混合在PHP中。使用双引号括起整个语句,单引号或 $ dbh-&gt; quote()实际上