如何让$ num_rows变量起作用

时间:2010-01-04 01:39:57

标签: php mysql pdo

我的代码中的所有内容都有效但$ num_results ...

    Book-O-Rama搜索结果     

Book-O-Rama搜索结果

    
    //create short variable names
    $searchtype=$_POST['searchtype'];
    $searchterm=$_POST['searchterm'];
    $searchterm=trim($searchterm);

    if (!$searchtype || !$searchterm) {
        echo 'You have not entered search details. Please go back and try again.';
        exit;
    }
    if (!get_magic_quotes_gpc()) {
        $searchtype = addslashes($searchtype);
        $searchterm = addslashes($searchterm);
    }

    include "../common/inc/db_connect_books.php";

    //$db = new mysqli_connect('localhost', 'root', '', 'books');

    $sql = "SELECT * FROM books WHERE ".$searchtype." LIKE '%".$searchterm."%'"
        or die(mysql_error());

    $result = $dbh->query($sql);

    $num_results = $result->num_rows;

    echo '<p>Number of books found: '.$num_results.'</p>';

    foreach ($result as $row) {
        echo '<p><b>'.($i+1).'. Title: ';
        echo htmlspecialchars(stripslashes($row['title']));
        echo '</b><br>Author: ';
        echo stripslashes($row['authors']);
        echo '<br>ISBN: ';
        echo stripslashes($row['isbn']);
        echo '<br>Price: ';
        echo stripslashes($row['price']);
        echo '</p>';

        $result->free();
        $dbh->close();
    }
?>

提供帮助

4 个答案:

答案 0 :(得分:2)

我使用mysql_num_rows http://php.net/manual/en/function.mysql-num-rows.php

<?php

$link = mysql_connect("localhost", "mysql_user", "mysql_password");
mysql_select_db("database", $link);

$result = mysql_query("SELECT * FROM table1", $link);
$num_rows = mysql_num_rows($result);

echo "$num_rows Rows\n";

?>

答案 1 :(得分:1)

我建议简单

$num_results = count($result);

答案 2 :(得分:1)

我猜你正在使用某种类型的MySQL包装器?如果是,则将$num_results = $result->num_rows;更改为$num_results = $dbh->num_rows;,假设num_rows是$dbh的方法。如果这不起作用,那么您可以使用$num_results = count($result);

我不确定你使用的包装器是什么,所以我不知道上面的代码是否有效。

答案 3 :(得分:0)

好吧,你在num_rows前面缺少“$”(即它应该是“$ num_rows”)。可能就是这样。