2017-09-25 52 views
1

我想在表中最后一行输出激活值,但我收到此错误:得到最后一行的值表

Catchable fatal error: Object of class mysqli_result could not be converted to string in C:\wamp64\www\mci_form\show.php on line 11

代码:

$hostName = 'localhost'; 
$userName = 'root'; 
$password = ''; 
$DBName = 'hamrahaval'; 
$connection = new mysqli($hostName,$userName,$password,$DBName); 
$query = "SELECT activation_code FROM subscribers ORDER BY id DESC LIMIT 1"; 
$result = $connection->query($query); 
echo $result; 
+0

'print_r($ result)';回声的东西,它需要是一个字符串,或可转换为字符串 – hlfrmn

+0

阅读此http://php.net/manual/en/mysqli-result.fetch-assoc.php –

+0

'$连接 - >查询'返回一个'mysqli_result'结果,你不能用'echo'打印它的数据。为此,你必须使用'print_r($ result)' –

回答

1

不能呼应结果。您必须使用此代码,因为结果不是字符串。

$query = "SELECT activation_code FROM subscribers ORDER BY id DESC LIMIT 1"; 
$result = $connection->query($query); 

if ($result->num_rows > 0) { 
    // output data of each row 
    while($row = $result->fetch_assoc()) { 
     echo "activation_code: " . $row["activation_code"]; 
    } 
} else { 
    echo "0 results"; 
} 
相关问题