title: Reading one record at a time by grand parent ID using conditional statement
body: <p>I have 3 tables suburbs, properties and images. suburb is a parent table
  of the property table in one to many relationship and image is a child
  table of property table in one to many relationship.</p>
  
  <p>I want to display one image, at a time, by suburb ID that will be sorted 
  (ORDER BY) by property ID.</p>
  
  <p>After iteration all the images are displayed correctly. But I can't 
  figure out how to display each row at a time using conditional statement by
  suburb ID (grand parent table ID).</p>
  
  <p>Below are test cases where I tried different decisions:</p>
  
  <h2>Test case 1: Iteration that displays all images</h2>
  
  <pre><code>$query = "SELECT i.*, p.property_id, s.suburb_id 
            FROM property_images i
            LEFT JOIN properties p
            ON i.property_id = p.property_id
            LEFT JOIN suburbs s
            ON p.suburb_id = s.suburb_id   
            WHERE s.suburb_id = 60                      
            ORDER BY p.property_id";
  
  $result = $conn-&gt;query($query);
  
  $images = array();
  
  foreach($result as $row){
     extract($row);
     $images[] = $image_location . '&lt;br /&gt;';
  }
  
  print_r($images);
  
  /*
    Output:
  
    Array ( [0] =&gt; public/images/properties/uploads/1.jpg
    [1] =&gt; public/images/properties/uploads/2.jpg
    [2] =&gt; public/images/properties/uploads/3.jpg
    [3] =&gt; public/images/properties/uploads/4.jpg
    [4] =&gt; public/images/properties/uploads/5.jpg
    [5] =&gt; public/images/properties/uploads/6.jpg
    [6] =&gt; public/images/properties/uploads/7.jpg
    [7] =&gt; public/images/properties/uploads/8.jpg
    [8] =&gt; public/images/properties/uploads/9.jpg
    [9] =&gt; public/images/properties/uploads/10.jpg
    [10] =&gt; public/images/properties/uploads/11.jpg
    [11] =&gt; public/images/properties/uploads/12.jpg
    [12] =&gt; public/images/properties/uploads/13.jpg
    [13] =&gt; public/images/properties/uploads/14.jpg
    [14] =&gt; public/images/properties/uploads/15.jpg
    [15] =&gt; public/images/properties/uploads/16.jpg
    [16] =&gt; public/images/properties/uploads/17.jpg
    [17] =&gt; public/images/properties/uploads/18.jpg
  )
  */
  </code></pre>
  
  <h2>Test case 2: Comparing $image_id if it's equal to its equivalent row</h2>
  
  <pre><code> $query = "SELECT i.*, p.property_id, s.suburb_id 
             FROM property_images i
             LEFT JOIN properties p
             ON i.property_id = p.property_id
             LEFT JOIN suburbs s
             ON p.suburb_id = s.suburb_id   
             WHERE s.suburb_id = 60                     
             ORDER BY p.property_id";
  
  $counter = mysqli_num_rows($result);
  
  $image_id = $row['property_image_id'];
  $image_location = $row['image_location'];
  
  if($counter &gt; 0){
       if($image_id == 1){
           print_r($image_location);
       }
   }
  
   //Here the first row is returned 
  
  /*
   Output:
  
    public/images/properties/uploads/1.jpg
  */
  </code></pre>
  
  <h2>Test case 3: Comparing $image_id if it's equal to row number 2</h2>
  
  <pre><code> $query = "SELECT i.*, p.property_id, s.suburb_id 
             FROM property_images i
             LEFT JOIN properties p
             ON i.property_id = p.property_id
             LEFT JOIN suburbs s
             ON p.suburb_id = s.suburb_id   
             WHERE s.suburb_id = 60                     
             ORDER BY p.property_id";
  
  $counter = mysqli_num_rows($result);
  
  $image_id = $row['property_image_id'];
  $image_location = $row['image_location'];
  
  if($conter &gt; 0){
       if($image_id == 2){
           print_r($image_location);
       }
   }
  
   //Here it returns null/empty 
  
  /*
   Output: (not output)
  
  */
  </code></pre>
  
  <p>I want to know why test case 2 returned the first row and test case 3 failed?</p>
  
  <p>I need to retrieve a single image separately for each query based on Suburb ID
  (grandparent ID)</p>
  
  <h2>Consider the Pseudo code below:</h2>
  
  <pre><code> 1. if image ID is equal to 1, return the first row of ImageURL.
   2. if image ID is equal to 2, return the second row of ImageURL. 
   3. if image ID is equal to 3, return the third row of ImageURL.
   4. else return the first row.
  </code></pre>
  
  <p>I mistakenly forgot to assign the result in the variable $row in the test case 2 and 3. When I tried to run the queries, it returned empty result set.</p>
  
  <p>The best approach was to use foreach loop to retrieve data from the result and test the equality of rows.
  The above approach is accomplished in the following code snippet. </p>
  
  <pre><code>$query = "SELECT i.*, p.property_id, s.suburb_id 
                        FROM property_images i
                        LEFT JOIN properties p
                        ON i.property_id = p.property_id
                        LEFT JOIN suburbs s
                        ON p.suburb_id = s.suburb_id   
                        WHERE s.suburb_id = 60                      
                        ORDER BY p.property_id";
              $result = $conn-&gt;query($query);
  
              $images = array();
  
  
              foreach($result as $row){
                  extract($row);
                  //$images[] = $image_location . '&lt;br /&gt;';
                  if($property_image_id == 1){
                      print_r($image_location);
                  }else if($property_image_id == 2){
                      print_r($image_location);
                  }else if($property_image_id == 3){
                      print_r($image_location);
                  }
              }
  </code></pre>