简体   繁体   English

使用PHP来计数MySQL数据库中的行?

[英]Using php to count rows in mysql database?

I am trying to count all rows from demos table but i got an error 我正在尝试计算演示表中的所有行,但出现错误
Catchable fatal error: Object of class mysqli_result could not be converted to string in C:\\xampp\\htdocs\\working_scripts\\test_2.php on line 8
My php code is: 我的PHP代码是:

<?php
$con=mysqli_connect("localhost","root","","test");
if (mysqli_connect_errno())
{
echo"Error connecting to database". mysqli_connect_error();
}
$comment_counter=mysqli_query($con,"SELECT COUNT(*) AS total FROM demos");
echo $comment_counter;
?>

You have to use mysqli_fetch_array 您必须使用mysqli_fetch_array

<?php
$con = mysqli_connect("localhost","root","","test");

if (mysqli_connect_errno())
{
    echo"Error connecting to database". mysqli_connect_error();
}

$result = mysqli_query($con, "SELECT COUNT(*) AS total FROM demos");

if($row = mysqli_fetch_array($result))
{
    echo $row["total"];
}
?>

Try this -> 试试这个->

<?php
$con=mysqli_connect("localhost","root","","test");
if (mysqli_connect_errno())
{
    echo"Error connecting to database". mysqli_connect_error();
}

$result = mysqli_query($con, 'SELECT COUNT(*) as total FROM demos');
if($row = mysqli_fetch_array($result))
    echo $row["total"];
?>

OR 要么

$result =  mysqli_num_rows(mysqli_query($con, 'SELECT * FROM demos'));
echo $result;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM