运行以下代码时出现此错误:
可捕获的致命错误: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/