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 this site and other web sources. However, I still cannot seem to fix it with the information I have found.
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 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. This is something I made from scratch to help me learn PHP.
Edit: Edited title again because of an influx of down votes from when I last changed the title to better detail my issue
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 error is:
if($query1->num_rows>0){
EDIT 2
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.
The only error it's running is that post_id is undefined, but that's because it's not defined until the statement below it- the statement it's supposed to be under.Otherwise, it will work exactly as I want.
However, I need it to exist within the while
loop in the above query to function how I need it so that each post preview can have it's unique image that was uploaded with it.
Could this be because I have a while
nested within another while
?
EDIT 3
Here is my images
table:
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;
And here is a visual of the information (sans img_type since it was off screen for the screenshot):
EDIT 4
I also wanted to mention that I have this exact code that I am trying to make work here, working without a hitch on another page. I would think it has to be something in this that has to be interfering with the code since I do not have the following on my other page where things are working fine:
<?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, '');
?>