-1

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):

enter image description here

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, '');
        ?>
Vel
  • 69
  • 7

1 Answers1

0

I am assuming you have saved a photo, not the photo name inside your database.So from that perspective i am answering.

Your current statement is

 echo "<img src='admin/images/".$imgrow->img_name."' width='150px' height='150px' >";

actually when an image is stored inside the database its stored in blob type , it doesn't store the name of the image , you have to use base64_encode() to retrieve blob data.

  echo "<img src='admin/images/".base64_encode($imgrow->img_name)."' width='150px' height='150px' >";

or being in one piece, i will change your code because it's vulnerable to sql injection. Use prepared statements.

 <?php
        $i = 0;
      ?>

    <?php 
        $imgsql = $db->prepare("SELECT img_name, img_path FROM images WHERE post_id = ? LIMIT 1");
        $imgsql->bind_param("s",$post_id);
        $imgsql->execute();
        $query1 = $imgsql->get_result();
        if($query1->num_rows>0){
          while ($imgrow = $query1->fetch_assoc()){  
          echo "<img src='admin/images/".base64_encode($imgrow['img_name'])."' width='150px' height='150px' >";
        }
      }
    ?> 
Adern Nerk
  • 332
  • 3
  • 13
  • I am storing the image name and image path in my database like this [link](https://i.imgur.com/hIErqfu.png) so I am not using any blob type in this particular case. I actaullt have this particular code working on another page (including the ` echo "";` to display images), so I am very confused as to why it is not working here. – Vel Feb 07 '18 at 02:49
  • I can get the entire code to work fine if I change the order and stick it above the segment where I have `$query`/the first `while` loop, but once I put it below that so it is called in that while loop, the '_trying to get property of non-object_' error occurs. The error never showed up on the other page where I was using this same statement. So I think the PHP segment above it is somehow interfering or vice versa. – Vel Feb 07 '18 at 02:49
  • I took your advice of using prepared statements for this code, and now that I am, I am returning additional errors as well as the same darn '_trying to get property of non-object_' as well as a '_Uncaught Error: Call to a member function execute() on boolean ... Stack trace: #0 {main} thrown_'. – Vel Feb 07 '18 at 02:49
  • However, if I place my prepared statement above the PHP segment where I'm running `$query`/the first `while` loop, that error goes away and it works 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. – Vel Feb 07 '18 at 02:49
  • I edited my original post to show the entire schema for my `images` table for reference. – Vel Feb 07 '18 at 03:10