PHP回显来自SQL查询的总行数

时间:2017-12-07 19:35:50

标签: php sql

我不确定我在哪里出错了。我在这里搜索过类似的问题而没有运气。任何帮助将不胜感激。谢谢!

$check = "SELECT Number FROM advisors";
$result = mysqli_query($check);
$count = mysqi_num_rows($result);

echo $count;

2 个答案:

答案 0 :(得分:1)

你应该像这样使用php prepare语句

$count  = 0;
$mysqli = new mysqli(host, dbUser, dbPassword, dbName);
mysqli_set_charset($mysqli, "utf8");
$sql    = "select count(*)  from advisors";
if ($stmt   = mysqli_prepare($mysqli, $sql))
{
    mysqli_stmt_execute($stmt);
    mysqli_stmt_store_result($stmt);
    mysqli_stmt_bind_result($stmt, $c);
    if (mysqli_stmt_fetch($stmt))
    {
        $count = $c;
    }
    mysqli_stmt_close($stmt);
}
return $count;

有关更多信息,请参阅php prepare语句的链接 Documentation of php prepare statement

答案 1 :(得分:0)

您应该在查询中使用COUNT并查看它是否有效, “SELECT COUNT(number)as number FROM advisors”; 顺便说一下,我注意到$ count中的拼写错误,应该是$ count = mysqli_num_count($ result)。