gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 13:04:37 25 4
gpt4 key购买 nike

我试图计算演示表中的所有行,但出现错误
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;
?>

最佳答案

您必须使用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"];
}
?>

关于php - 使用php计算mysql数据库中的行数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22899962/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com