PHP / MySQL中的计数器

时间:2016-06-22 02:22:48

标签: php mysql count

所以我对PHP很陌生,我想实现一个计数器。我的情况是,例如,如果我输入单词" dog"在我正在开发的网站的搜索栏中,它将显示我的数据库中所有狗的列表,并在列表顶部,它将显示有多少匹配,说"狗的数量: " ..这是迄今为止代码的摘录。除了列出比赛(来自我制作的另一个工作函数)之外,它没有做任何事情。

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>";
}

3 个答案:

答案 0 :(得分:0)

$q="SELECT count(*) as dogsno 
    FROM tblanimal 
    where name='dog'";

它将计算狗的数量。

答案 1 :(得分:0)

您无需迭代查询中返回的行数。只需使用mysql_num_rows()

尝试这样做:

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

答案 2 :(得分:-1)

这样做

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