PHP 和 MySQL 错误 : Object of class mysqli_result could not be converted to string

标签 php mysqli

<分区>

我收到错误:

Object of class mysqli_result could not be converted to string.

代码:

<?php
  $con=mysqli_connect("78.46.51.231","root","","multicraft_daemon");
  if (mysqli_connect_errno($con)){
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

  $sql = ("select sum(`memory`) from `server`;");

  $result = mysqli_query($con, $sql);

  echo $result;    //$result is mysqli_result and can't be forced to string.
?>

正确的做法是什么?

最佳答案

您不能直接输出查询结果。使用:

$sql = ("select sum(`memory`) AS memTotal from `server`");
// Show used memory
$result = mysqli_query($con, $sql);
echo $result->fetch_object()->memTotal;

$result 变量包含一个对象(类型为 mysqli_result),您可以从中获取需要输出的标量。

关于PHP 和 MySQL 错误 : Object of class mysqli_result could not be converted to string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16195840/

相关文章:

php - 如何在 PHP 中连接 mysql 中的两列

php - 自定义wordpress小部件发生错误

php - mysql_query() : Access denied for user '' @'localhost'

php - PHP提交为空白,无 echo

php:从自己的 mysli 类获取数据库名称

php - 如何在 mysql 查询 php 中回显当前日期

php - Jquery onblur 第二次不工作

php - mysqli_commit 失败会自动回滚吗?

PHP mysql 到 mysqli 的迁移问题(自定义函数、程序样式)

php - Mysql 2跨 TableView 不将1=1关系转化为一个 View