如何在php中显示mysql表的总数

时间:2014-02-15 15:15:11

标签: php mysql

我正在尝试使用以下代码计算总行数:

$countvideo = "SELECT count(id) FROM `videos`";
$countvideo_run = mysqli_query($connect, $countvideo);
if($countvideo_run){
        $countvideo_result = mysql_result($countvideo_run, 0, 'count(id)');
    }

但它显示了我的错误:

  

警告:mysql_result()要求参数1为资源,对象在第121行的C:\ xampp \ htdocs \ task \ media \ new \ adminpanel.php中给出

我怎么算这个?

3 个答案:

答案 0 :(得分:1)

您正在将mysqli_返回对象与不兼容的mysql_函数混合。

$countvideo = "SELECT count(id) qty FROM `videos`";
if (($result = mysqli_query($connect, $countvideo)) !== false) {
    $row = $result->fetch_assoc();
    $countvideo_result = $row['qty'];
}

答案 1 :(得分:0)

您正在混合mysqlimysql个功能

$countvideo = "SELECT count(id) as `total` FROM `users`";
$countvideo_run = mysqli_query($connect, $countvideo);
if ($countvideo_run) {
    $countvideo_result = $countvideo_run->fetch_object();
    $total = $countvideo_result->total;
} 

答案 2 :(得分:0)

您使用mysql_result代替mysqli_result