Jump to content

MySQL Left Join, PHP Array Question


devilindisguise

Recommended Posts

Hello all

 

I have the need to join up two tables and display the output in a table. These tables are a 'Site Details' table and a 'Customer Details' table where the 'cust_id' (PK on the CD table and FK on the SD table) match.

 

The MySQL statement (within PHP) I have works a treat in as much as the join seems to do the trick and join the two up on the correct row:

<?php
if($letter == 'ALL')
{$query = mysqli_query($con,
SELECT site_details.site_id, site_details.sitename, site_details.sitecode, 
site_details.postcode,customer_details.cust_number, customer_details.cust_fname 
FROM site_details 
LEFT JOIN customer_details ON site_details.cust_id = customer_details.cust_id
ORDER BY sitename ASC);
}
 
I then display this data in a table as follows:
              <table class="table table-striped table-hover">
                <thead>
                  <tr>
                    <th>Site Code</th>
                    <th>Site Name</th>
                    <th>Postcode</th>
                    <th>Contact Name</th>
                    <th>Contact Number</th>
                  </tr>
                </thead>
                <tbody>
Then want to display the records in the tables:
 
                  <?php 
                  while($row = mysqli_fetch_assoc($query))
                  { 
                  ?>                  
                  <tr>
                    <td><?php echo $row['sitecode']; ?></td>
                    <td><?php echo $row['sitename']; ?></td>
                    <td><?php echo $row['postcode'];?></td>
                    <td><?php echo $row['cust_fname']; ?></td>
                    <td><?php echo $row['cust_number']; ?></td>
                  </tr>
                  <?php } ?>
                </tbody>
              </table>

Where my lack of understanding lies are with arrays which I'm suspecting is part of the problem. With the above it echoes out the details from the 'Site Details' table no problem. I just can't get anything from the 'Customer Details' table.

 

The answer as ever is probably staring me in the face. I would appreciate if someone could advise on where I'm going wrong please?

Many thanks

Link to comment
https://forums.phpfreaks.com/topic/293454-mysql-left-join-php-array-question/
Share on other sites

Another thing that wastes people's time is when posters post code that they didn't actually run. The code you posted here could not possibly have run since your query string is not in quotes.

 

Please don't re-type code to post. Instead use copy/paste methods to ensure that the code you are posting is the same code you ran

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.