簡體   English   中英

使用PHP來計數MySQL數據庫中的行?

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

我正在嘗試計算演示表中的所有行,但出現錯誤
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
$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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM