2013-04-24 108 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

相關問題