开发者

PDO MYSQL Question

I am having difficulty in setting up this function. Probably a very easy fix however I'm just starting to use PDO and I am unsure as to how to get this working.

My problem is here

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

whenever the code executes this is arrives at

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

All help is greatly appreciated

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 = "$keywor开发者_如何学God"
                       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.";


Your SQL is wrong, therefore your $STH doesn't get a result object and was set to false instead.

SQL strings need ' single quotes. You mixed that up in PHP. Use double quotes to enclose the whole statement, and single quotes or $dbh->quote() actually within.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜