devilindisguise Posted December 28, 2014 Share Posted December 28, 2014 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 Quote Link to comment https://forums.phpfreaks.com/topic/293454-mysql-left-join-php-array-question/ Share on other sites More sharing options...
Barand Posted December 28, 2014 Share Posted December 28, 2014 With a left join, cust_fname and cust_number will be NULL where there is no customer record matching the site record Quote Link to comment https://forums.phpfreaks.com/topic/293454-mysql-left-join-php-array-question/#findComment-1500964 Share on other sites More sharing options...
devilindisguise Posted December 28, 2014 Author Share Posted December 28, 2014 Thanks Barand for your reply. Think I need to file this one under dufus. Seems there were no customer records to pull out, I thought there were. So yes, your comment was right. Apologies for wasting people's time. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/293454-mysql-left-join-php-array-question/#findComment-1500971 Share on other sites More sharing options...
ginerjm Posted December 28, 2014 Share Posted December 28, 2014 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 Quote Link to comment https://forums.phpfreaks.com/topic/293454-mysql-left-join-php-array-question/#findComment-1500975 Share on other sites More sharing options...
devilindisguise Posted December 28, 2014 Author Share Posted December 28, 2014 Ginerjm Understood mate. A wrapping of the knuckles was to be expected. Quote Link to comment https://forums.phpfreaks.com/topic/293454-mysql-left-join-php-array-question/#findComment-1500978 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.