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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.