我正在尝试从演示表中计算所有行,但是我收到了错误
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
我的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;
?>
答案 0 :(得分:1)
您必须使用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"];
}
?>
答案 1 :(得分:0)
试试这个 - &gt;
<?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;