【问题标题】:How to increment the search count in php/mysql?如何增加 php/mysql 中的搜索计数?
【发布时间】:2013-10-20 13:05:32
【问题描述】:

我有一个表包含几个字段(idfirstnamesurnameusernamesearch_count

我已经构建了一个小型搜索引擎,可以搜索我的表以查找firstnamesurname 中存在的任何匹配项,并且我得到的结果没有问题。

现在,我要做的是每次匹配时将 search_count 字段增加 1!

例如,假设我们有下表users

id | firstname | surname  | username | search_count 
1  | John      | Mike     | un1      | 0
2  | John      | Jeff     | un2      | 0
3  | Dale      | John     | un3      | 0
4  | Mike      | Gorge    | un4      | 0

假设我们正在搜索 Jeff 作为关键字

所以,查询将返回 1 条记录

我想要做的是将search_count 增加1 以获取匹配记录

所以结果会是这样的:

id | firstname | surname  | username | search_count 
2  | John      | Jeff     | un2      | `1`

如果我们进行新的搜索(例如John),结果应该是这样的:

id | firstname | surname  | username | search_count 
1  | John      | Mike     | un1      | 1
2  | John      | Jeff     | un2      | 2
3  | Dale      | John     | un3      | 1

我尝试了几种方法,但都没有成功。所以我感谢任何提示和帮助

这是我的代码...

<?php
// open the HTML page
include 'html_open.php';
// require the db connection
require '/inc/db.inc.php';
// require the error messages
require '/inc/echo.inc.php';



    if (isset($_GET['keyword'])) {
        $keyword = $_GET['keyword'];
        if (!empty($keyword)) {
            // build our search query
            $search_query = "SELECT * FROM `users` WHERE `firstname` = '".mysql_real_escape_string($keyword)."' OR `surname` = '".mysql_real_escape_string($keyword)."' ORDER BY `search_count` DESC";
            // run the search query
            $search_query_run = mysql_query($search_query);
            // search results
            $search_results_num = mysql_num_rows($search_query_run);
            // check query return results
            if ($search_results_num>0) {        
                echo 'Search engine returns <strong>[ '.$search_results_num.' ]</strong> result(s) for <strong>[ '.$keyword.' ]</strong>:<br>';
                // retrieving the information found
                echo '<ol>';
                while ($search_result_information = mysql_fetch_assoc($search_query_run)) {
                    //$current_search_count = ;
                    echo '<li>'.$search_result_information['username'].'.  This user has been searched: '.$search_result_information['search_count'].' times before.</li>';
                }
                echo '</ol><hr>';
                include 'search_form.php';
            } else {
                echo '<hr>Search engine returns no result for <strong>[ '.$keyword.' ]</strong>, please try another keyword.<hr>'; // hint: no result found
                include 'search_form.php';
            }               
        } else {
            echo $err20_002; // hint: must insert input
            include 'search_form.php';
        }
    } else {
            echo $err20_001; // hint: form has not been submitted
            include 'search_form.php';
    }
// close the HTML page  
include 'html_close.php';   
?>

附:我是 PHP / MySQL 的新手,这是我的第一个代码 :)

【问题讨论】:

    标签: php mysql count increment


    【解决方案1】:
    ...
    while ($search_result_information = mysql_fetch_assoc($search_query_run)) {
        # add this following line
        mysql_query('UPDATE `users` SET search_count=search_count+1 WHERE id='.$search_result_information['id']);
        # Edit. Change result to show new number
        $search_result_information['search_count']++; # this adds 1 to the value (does not affect stored data in database)
        echo '<li>'.$search_result_information['username'].'.  This user has been searched: '.$search_result_information['search_count'].' times before.</li>';
    }
    ...
    

    【讨论】:

    • 感谢这项工作,但有没有办法在显示回声之前增加计数器?
    • 如果没有比必要的更多的数据库请求,最简单和合乎逻辑的答案是在输出之前对该值 +1。将编辑我的答案。
    【解决方案2】:

    在您的情况下,您需要获取找到的值并执行更新语句,将 +1 添加到 search_count

    $search_query = "SELECT id, firstname, surname, username, search_count FROM `users` WHERE `firstname` = '".mysql_real_escape_string($keyword)."' OR `surname` = '".mysql_real_escape_string($keyword)."' ORDER BY `search_count` DESC";
    $search_query_run = mysql_query($search_query);
            // search results
            $search_results_num = mysql_num_rows($search_query_run);
            // check query return results
            if ($search_results_num>0) {        
                echo 'Search engine returns <strong>[ '.$search_results_num.' ]</strong> result(s) for <strong>[ '.$keyword.' ]</strong>:<br>';
                // retrieving the information found
                echo '<ol>';
                while ($search_result_information = mysql_fetch_assoc($search_query_run)) {
                    //$current_search_count = ;
                    $update_search = "UPDATE users SET search_count = search_count + 1 WHERE id = {$search_result_information['id']}"; // so every `id` will increment its search_count with 1. You will need to select the rows once again, to take this count, or to manually increment in PHP
                    mysql_query($update_search);
                    echo '<li>'.$search_result_information['username'].'.  This user has been searched: '.$search_result_information['search_count'].' times before.</li>'; 
    

    P.S.:强烈不推荐使用 mysql_* lib。从官方文档http://www.php.net/manual/en/function.mysql-query.php的红框可以看出,你应该选择当前实际支持的API之一

    【讨论】:

    • 感谢 P.S.,我知道,但我想我会先让我的代码使用 mysql_*,因为我发现它很容易理解,然后将其更新为 mysqli_*
    • 是的,它工作正常,谢谢,但是有没有办法在回显结果之前更新搜索计数?
    • 如果你想返回新的计数,你可以对每个结果只回显+1,或者在你的选择语句中为列结果添加+1。这是我能想到的最短的。更长的是进行选择,遍历行并更新。然后再做一次选择,然后回显。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多