Run a nested loop sql statement

Run a nested loop sql statement


Right now is printing out the unit and a connected image every time there is an image associated with it. It should print out each listing only once and then loop through the photos that correspond to that listing. This reminds me of a nested loop but I dont know how to run it in an sql statement.

The second issue w/ this application is the code is excluding any unit that client does not attach pictures on upload, It needs to print unit info regardless if user uploaded files or not.
I would be grateful for any help or advice.

Current application state

Photos Table Query

Monticel Table Query

Join QUERY Table

Here is the code:

	$query = 'SELECT m.UNIT, m.SIZE,, m.PRICE, p.image_name FROM 8052monticello m INNER JOIN photos p ON p.fk_unit = m.unit'; 
if ($r = mysqli_query($connection, $query)) { // Run the query.
	// Retrieve and print every record:
	while ($row = mysqli_fetch_assoc($r)) {
	  if ($row['UNIT']){
        print "<img src=\"images/{$row['image_name']}\" width='100'>
        <p><h3>{$row['UNIT']} Unit #</h3>    
        {$row['SIZE']} Sq Feet<br>
        {$row['PRICE']} Monthly rent<br>
        <a href=\"edit_UNIT.php?id={$row['id']}\">Edit</a>
        <a href=\"delete_UNIT.php?id={$row['id']}\">Delete</a>


i hope someone can tell me how to solve this, I was originally thinking you need to run two different queries. I do not know. any input/hints/advice is welcome.