使用php计算mysql数据库中的行数?

使用php计算mysql数据库中的行数?,php,mysql,Php,Mysql,我正在尝试从演示表中计算所有行,但出现错误 Catchable致命错误:在第8行的C:\xampp\htdocs\working\u scripts\test\u 2.php中,类mysqli\u result的对象无法转换为字符串 我的php代码是: <?php $con=mysqli_connect("localhost","root","","test"); if (mysqli_connect_errno()) { echo"Error connecting to database

我正在尝试从演示表中计算所有行,但出现错误
Catchable致命错误:在第8行的C:\xampp\htdocs\working\u scripts\test\u 2.php中,类mysqli\u result的对象无法转换为字符串
我的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;
?>

您必须使用


试试这个->

<?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"];
?>

您应该在
COUNT
函数中传递列名@基本需要,否,
count(*)
完全有效。哇!我不知道!那么,你知道问题是什么吗Dmysqli_查询返回一个对象:是的,它可以工作,但是mind-404已经解释过了。谢谢anway:D
<?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"];
?>
$result =  mysqli_num_rows(mysqli_query($con, 'SELECT * FROM demos'));
echo $result;