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

use*_*211 3 php mysql sql select count

我收到错误:

Object of class mysqli_result could not be converted to string.
Run Code Online (Sandbox Code Playgroud)

码:

<?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.
?>
Run Code Online (Sandbox Code Playgroud)

这样做的正确方法是什么?

bwo*_*ebi 12

您无法直接输出查询结果.使用:

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

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

  • 这帮我了!非常感谢你! (2认同)