I have a list of blog posts where the first image of a set uploaded with the post information is displayed as a thumbnail on the list. I am currently running into a ‘trying to get property of non-object’ error, and I think that’s the only thing that’s preventing this from working the way I want it to.
I want to preface this with yes, I have read up on how to fix this error and have been making attempts at modifying my code based on the information I have found both on various web sources. However, I still cannot seem to get it working.
I’ve been working at this for a couple hours and I would really appreciate if anyone could help steer me in the right direction. I’m still in the process of getting my feet wet with PHP, so I am aware that I have a lot to learn still.
This is a non-wordpress blog I am working with.
Title Edit: Edited title to better explain the issue I am having
Here is my code in it’s entirety from the part of the html where the post previews are being generated:
<?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?>
Here is the portion where I am displaying the preview image:
<?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' >";
}
}
?>
This is the specific line where the ‘trying to get property of non-object’ error is:
if($query1->num_rows>0){
EDIT
When I run var_dump($query1);
it returns bool(false)
I think the query $query
I’m running above it is causing issues. Just out of curiosity, I placed
<?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' >";
}
}
?>
above
<?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, '');
?>
and an image displayed and it worked fine. However, I need it to exist within the while
loop in the above query to function how I need it. Could this be because I have a while
nested within another while
?