2013-04-24 38 views
3

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

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

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

回答

12

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

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

$result该变量保存的对象(类型mysqli_result的),从中可以提取需要输出的标量。

+2

这帮了我一把!非常感谢你! – user2316211 2013-04-24 15:24:47

3

$result是一个结果对象。从手册中获取mysqli_query()

Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object . For other successful queries mysqli_query() will return TRUE.

+1

+1阅读手册。 – Kermit 2013-04-24 15:24:49

+0

谢谢您的信息! – user2316211 2013-04-24 15:25:06

相关问题