gpt4 book ai didi

mysql - 尝试对记录进行计数时,无法将mysqli_result类的对象转换为字符串

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

运行以下代码时出现此错误:

可捕获的致命错误:mysqli_result类的对象无法转换为字符串..

但是当我直接在mysql中运行查询时,它的值为12

谁能帮我告诉我我做错了什么?谢谢

<?
include 'clx.php';

// Create connection
$conn = mysqli_connect($server, $user_name, $pass_word,$database);

//Check connection

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

// sending query to count records
$table = 'USERS';
$query1 = "SELECT COUNT(*) FROM {$table}";

echo $query1;

$result = MYSQLI_QUERY($conn,$query1);
IF (!$result) {
DIE("Query to show fields from table failed");
}

echo $result; //this is where I get the error apperently $result is an array or an object but
why?
and when I use the same query directly in mysql it gives me a value of 12 (the
number of records in the table)

if ($result== 0){
echo("No results found matching your query - $query1"); // bah, modify the "Not Found" error
for your needs.
exit();}

>

最佳答案

因为MYSQLI_QUERY失败时返回false,成功时返回object。您不能直接打印对象。

所以试试这个:

if ($result)
{
// Fetch one and one row
while ($row=mysqli_fetch_row($result))
{
echo $row[0];
}
}

关于mysql - 尝试对记录进行计数时,无法将mysqli_result类的对象转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26986850/

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