Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP/MySQL中的计数器_Php_Mysql_Count - Fatal编程技术网

PHP/MySQL中的计数器

PHP/MySQL中的计数器,php,mysql,count,Php,Mysql,Count,所以我对PHP很陌生,我想实现一个计数器。我的场景是,例如,如果我在我正在开发的网站的搜索栏中键入单词“dog”,它将显示我数据库中所有狗的列表,并且在列表的顶部,它将显示有多少匹配项,比如“Number of dogs:。。以下是迄今为止代码的摘录。除了列出匹配项(来自我制作的另一个工作函数),它什么都不做 function countStores($conn, $match) { $columns = "FieldResearcher, Mall, NameOfStore, Floo

所以我对PHP很陌生,我想实现一个计数器。我的场景是,例如,如果我在我正在开发的网站的搜索栏中键入单词“dog”,它将显示我数据库中所有狗的列表,并且在列表的顶部,它将显示有多少匹配项,比如“Number of dogs:。。以下是迄今为止代码的摘录。除了列出匹配项(来自我制作的另一个工作函数),它什么都不做

function countStores($conn, $match)
{

   $columns = "FieldResearcher, Mall, NameOfStore, Floor, Building, AMEX";
   $table          = "final";
   $conditions   = "NameOfStore LIKE '%$match%' ";

    $query = "SELECT count(NameOfStore) FROM $table where $conditions";


    $result = mysqli_query($conn, $query);
    if ($result == FALSE)
    {
        echo "Invalid query: " . $conn->error;
        echo "<br/>";
        return;
    }

    echo "Number of Stores: " . $query;

     $actionStr = $_SERVER['PHP_SELF'];                          // TODO

    while ( $row = mysqli_fetch_row($result) )
    {
        echo "<tr>";
        for ($i = 0; $i < count($row); $i++)
        {
            echo "<td>" . $row[$i] . "</td>";
        }

        echo "<td>";                                            // TODO
        echo "<form method=\"POST\" action=\"$actionStr\">";    // TODO
        echo "<input type=\"hidden\" name=\"code\" value=\"$row[0]\">";
        //echo "<input type=\"submit\" value=\"BUY!\">";          // TODO
        echo "</form>";                                         // TODO
        echo "</td>";                                           // TODO
        echo "</tr>";
    }
    echo "</tbody>";
    echo "</table>";
}
函数countStores($conn,$match)
{
$columns=“美国运通商场、店名、楼层、大楼的FieldResearch”;
$table=“final”;
$conditions=“类似于“%$match%”的NameOfStore;
$query=“从$table中选择count(NameOfStore),其中$conditions”;
$result=mysqli\u查询($conn,$query);
如果($result==FALSE)
{
回显“无效查询:”.$conn->错误;
回声“
”; 返回; } echo“门店数量:”.$query; $actionStr=$\u服务器['PHP\u SELF'];//待办事项 while($row=mysqli\u fetch\u row($result)) { 回声“; 对于($i=0;$i
像这样做

$query = "SELECT COUNT(*) as count FROM $table WHERE $conditions";
$result = mysqli_query($conn, $query);
$row = mysqli_fetch_row($result);
$count = $row['count'];

它将计算狗的数量。

您不需要迭代查询中返回的行数。就用吧

试着这样做:

$result = mysql_query("SELECT * FROM table_name", $dbConn);
$num_rows = mysql_num_rows($result);

只需使用手册中的
num_rows
$result = mysql_query("SELECT * FROM table_name", $dbConn);
$num_rows = mysql_num_rows($result);