【发布时间】:2018-07-16 23:26:35
【问题描述】:
我有一个博客帖子列表,其中包含帖子信息的一组上传的第一张图片在列表中显示为缩略图。我目前遇到了“试图获取非对象的属性”错误,我认为这是阻止它按我想要的方式工作的唯一原因。
我想以“是”作为开头,我已阅读有关如何修复此错误的信息,并一直在尝试根据我在本网站和其他网络资源上找到的信息来修改我的代码。但是,我似乎仍然无法用我找到的信息来修复它。
我已经为此工作了几个小时,如果有人能帮助我朝着正确的方向前进,我将不胜感激。我仍然对 PHP 感兴趣,所以我知道我还有很多东西要学。
这是我正在使用的非 wordpress 博客。这是我从零开始帮助我学习 PHP 的东西。
编辑:再次编辑标题,因为自从我上次更改标题以更好地详细说明我的问题时,大量的反对票涌入
这是我的完整代码,来自生成帖子预览的 html 部分:
<?php
$queryString = "SELECT post_id, title, price, image, LEFT(description, 180) AS description, category FROM post INNER JOIN categories ON categories.category_id=post.category_id ORDER BY post_id DESC LIMIT $start, $per_page";
$query = $db->prepare($queryString);
$query->execute();
$query->bind_result($post_id, $title, $price, $image, $description, $category);
while ($query->fetch()):
$lastspace = strrpos($description, '');
?>
<article>
<div class="preview">
<?php
$i = 0;
?>
<?php
$imgsql = "SELECT img_name, img_path FROM images WHERE post_id = '$post_id' LIMIT 1";
$query1 = $db->query($imgsql);
if($query1->num_rows>0){
while ($imgrow = $query1->fetch_object()){
echo "<img src='admin/images/".$imgrow->img_name."' width='150px' height='150px' >";
}
}
?>
<div class="info">
<h2><?php echo $title?></h2>
<div class="price">
$<?php echo $price?>
</div>
<div class="cat">
<?php echo $category?>
</div>
<div class="description">
<?php echo substr($description, $lastspace).'...<br><a href="post.php?id='.$post_id.'">VIEW PRODUCT</a>'?>
</div>
</div>
</div>
</article>
<?php endwhile?>
这是我显示预览图像的部分:
<?php
$i = 0;
?>
<?php
$imgsql = "SELECT img_name, img_path FROM images WHERE post_id = '$post_id' LIMIT 1";
$query1 = $db->query($imgsql);
if($query1->num_rows>0){
while ($imgrow = $query1->fetch_object()){
echo "<img src='admin/images/".$imgrow->img_name."' width='150px' height='150px' >";
}
}
?>
这是错误所在的特定行:
if($query1->num_rows>0){
编辑 2
当我运行var_dump($query1); 时,它会返回bool(false)
我认为我在上面运行的查询$query 会导致问题。只是出于好奇,我放了
<?php
$imgsql = "SELECT img_name, img_path FROM images WHERE post_id = '$post_id' LIMIT 1";
$query1 = $db->query($imgsql);
if($query1->num_rows>0){
while ($imgrow = $query1->fetch_object()){
echo "<img src='admin/images/".$imgrow->img_name."' width='150px' height='150px' >";
}
}
?>
上面
<?php
$queryString = "SELECT post_id, title, price, image, LEFT(description, 180) AS description, category FROM post INNER JOIN categories ON categories.category_id=post.category_id ORDER BY post_id DESC LIMIT $start, $per_page";
$query = $db->prepare($queryString);
$query->execute();
$query->bind_result($post_id, $title, $price, $image, $description, $category);
while ($query->fetch()):
$lastspace = strrpos($description, '');
?>
并显示图像并且效果很好。
它运行的唯一错误是 post_id 未定义,但这是因为直到它下面的语句才定义它 - 它应该在下面的语句。否则,它将完全按照我的意愿工作。
但是,我需要它存在于上述查询中的 while 循环中,以发挥我的需要,以便每个帖子预览都可以拥有与它一起上传的唯一图像。
这可能是因为我有一个 while 嵌套在另一个 while 中吗?
编辑 3
这是我的images 表:
CREATE TABLE `images` (
`img_id` int(11) NOT NULL,
`post_id` int(11) NOT NULL,
`img_path` varchar(500) NOT NULL,
`img_type` varchar(500) NOT NULL,
`img_name` varchar(500) NOT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
这是信息的视觉效果(没有 img_type,因为它不在屏幕截图中):
编辑 4
我还想提一下,我有这个确切的代码,我正试图在这里工作,在另一个页面上工作顺利。我认为它必须是其中的某些东西必须干扰代码,因为我在其他运行良好的页面上没有以下内容:
<?php
$queryString = "SELECT post_id, title, price, image, LEFT(description, 180) AS description, category FROM post INNER JOIN categories ON categories.category_id=post.category_id ORDER BY post_id DESC LIMIT $start, $per_page";
$query = $db->prepare($queryString);
$query->execute();
$query->bind_result($post_id, $title, $price, $image, $description, $category);
while ($query->fetch()):
$lastspace = strrpos($description, '');
?>
【问题讨论】:
-
你能在该行之前发布一个
var_dump($query1);吗? -
@user2464424 它正在返回
bool(false) -
显然查询失败并出现无提示错误。 Check this answer 捕获错误并查看它。
-
@user2464424 我必须找到一个 mysqli 版本,因为我目前不使用 PDO,但谢谢!我会调查的。
-
那可能是mysqli_error().
标签: php