我收到错误消息:
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),您可以从中获取需要输出的标量。
$result