PHP& MySQL - 如何计算记录数

时间:2010-07-21 04:25:57

标签: php mysql

好的,我有这个网站搜索脚本,我正在计算当用户输入搜索词或术语时将显示多少条记录。我想知道如果我不知道会输入什么关键字,我怎么能这样做呢?

以下是我的搜索查询。

这是我的PHP&部分。 MySQL搜索代码。

$search_explode = explode(" ", $search);

foreach($search_explode as $search_each) {
    $x++;
    if($x == 1){
        $construct .= "article_content LIKE '%$search_each%' OR title LIKE '%$search_each%' OR summary LIKE '%$search_each%'";
    } else {
        $construct .= "OR article_content LIKE '%$search_each%' OR title LIKE '%$search_each%' OR summary LIKE '%$search_each%'";
    }

}

$construct = "SELECT users.*, users_articles.* FROM users_articles
              INNER JOIN users ON users_articles.user_id = users.user_id
              WHERE $construct";

这是第一个错误。

MySQL Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT users.*, users_articles.* FROM users_articles INNER JOIN users O' at line

这是第二个错误。

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given

2 个答案:

答案 0 :(得分:2)

FROM之后的SQL查询部分存储在变量中,然后您可以使用COUNT(*)来获取行数:

$search_explode = explode(" ", $search);

foreach($search_explode as $search_each) {
    $x++;
    if($x == 1){
        $construct .= "article_content LIKE '%$search_each%' OR title LIKE '%$search_each%' OR summary LIKE '%$search_each%'";
    } else {
        $construct .= "OR article_content LIKE '%$search_each%' OR title LIKE '%$search_each%' OR summary LIKE '%$search_each%'";
    }

}

$from = "FROM users_articles
         INNER JOIN users ON users_articles.user_id = users.user_id
         WHERE $construct";

$count_query = "SELECT COUNT(*) " . $from;

$result = mysql_query($count_query);
$count_result = mysql_fetch_row($result);
$count = $count_result[0];

答案 1 :(得分:1)

您可能还想查看mysql_num_rows()