小编典典

PHP和MySQL错误:无法将类mysqli_result的对象转换为字符串

sql

我收到错误消息:

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.
?>

正确的方法是什么?


阅读 199

收藏
2021-03-10

共1个答案

小编典典

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

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

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

2021-03-10