Having issues getting image to display on page because of non-object error (non-wordpress)

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?

It really looks like your query is invalid. As you already have the statement in the code, echo it out and run it in another tool like PHPMyAdmin or something - Maybe one of your variables is missing or causing a syntax error. You should consider using Prepared Statements for security reasons, that should be a minor step as you already use PDO, the only change would be to replace the variables with a placeholder and providing them within the execute() like

        $imgsql = "SELECT img_name, img_path FROM images WHERE post_id = ? LIMIT 1";
        $query1 = $db->prepare($imgsql);
        $query1->execute([$post_id]);
1 Like

Have a look at adding store_result() after your first query to buffer the results. I have read in various places (I don’t use mysqli myself, isn’t this mysqli rather than PDO with bind_result?) that there is an issue with running a second query before you have consumed the results from the first query. Another way would be to retrieve all the results into an array, then foreach() through it for your second query.

Can’t you use a JOIN to merge the two queries into one? I’m not sure how you’d get it to only pick one image.

1 Like

I had a look at implementing store_result() and that was exactly what I needed to do so solve this issue. Thank you so much! I’m surprised I didn’t come across it before. (And yes this is mysqli rather than PDO)

I was thinking about using JOIN to merge the queries, but I was running into issues with it getting to select only one image, so store_result() worked out much better in the end.

I should’ve disclaimed I wasn’t too worried with using prepared statements since I just wanted to get this to work, but now that I have it working, I am definitely going to use a prepared statement for it. Thanks!

Glad it’s working.

As for combining the queries, another way might be something like using a sub-query:

select post_id, title, price, image, 
  (select img_name from images where images.post_id = '$post_id' limit 1) as img_name,
  (select img_path from images where images.post_id = '$post_id' limit 1) as img_path,
  left(description, 180 ) as description,
  category from post inner join categories on categories.category = '$post_id' limit 1

I’m not sure whether there’s a way to get both sub-queries combined into one. You might need to specify the order in the subquery to ensure that the two results come from the same row.

1 Like

Okay, I’m gonna try that out and see what I can get working so I can use something like that in the future. Thanks!

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.