获取表中最后一行的值

时间:2017-09-25 11:20:05

标签: php mysql

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

  

捕获致命错误:第11行的C:\ wamp64 \ www \ mci_form \ show.php中无法将类mysqli_result的对象转换为字符串

代码:

$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;

1 个答案:

答案 0 :(得分: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";
}