【发布时间】:2015-05-15 09:22:08
【问题描述】:
我在编写文章系统时遇到错误。我想附和最新的文章。如果有更简单的方法可以做到这一点,请告诉我。 这是错误:
可捕获的致命错误: mysqli_result 类的对象无法在第 5 行的 /public_html/cms/articles.php 中转换为字符串
这是我的代码(来自articles.php):
<?php
require 'connect.php';
$getmax = "SELECT id FROM news ORDER BY id LIMIT 1";
$max = mysqli_query($conn, $getmax);
$max = (string)$max;
$sql = "SELECT * FROM news WHERE id=$max";
$result = mysqli_query($conn, $sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$articleOneTitle = $row["title"];
$articleOneDesc = $row["description"];
$articleOneContent = $row["content"];
}
} else {
echo "Sorry! - No articles are currently available, please check back later.";
}
echo $max;
?>
【问题讨论】:
-
$max = (string)$max;不计算。请阅读如何正确处理来自mysqli_query的结果。