Cyberspace Posted April 6, 2008 Share Posted April 6, 2008 I am having difficulty with displaying values from two tables. My code is: <?php $WalkNo = $_GET['WalkNo']; $query = "SELECT * FROM walker WHERE WalkNo='$WalkNo'"; $result = mysql_query($query); if(!$result){ // check if is something wrong print "Error"; }else{ if(mysql_num_rows($result) == 0){ print "No Participants exist"; }else{ while ($account = @mysql_fetch_array($result)) { $WalkNo=$account["WalkNo"]; ?> <table class="join" cellspacing="0" width="861"> <tr> <td class="Header">WalkNo</td> <td class="Header">Forename</td> <td class="Header">Surname</td> <td class="Header">DateJoined</td> </tr> <tr> <td class="Body"><?php echo $account["WalkNo"]; ?></td> <td class="Body"><?php echo $account["Forename"]; ?></td> <td class="Body"><?php echo $account["Surname"]; ?></td> <td class="Body"><?php echo $account["DateJoined"]; ?></td> </tr> <br> </table> <?php } } } ?> I the code at the min selects values from a table called "walker" which stores the following fields: WalkNo - number of walk the person is participating in MemberRef - person member number DateJoined - date member signed up for the walk programme I need to get the Members "Forename" and "surname" from the Members table based on the MemberRef in the "Walks" table, the above code works ok but im not sure how to select the members name from the members table, do i need two queries? Quote Link to comment https://forums.phpfreaks.com/topic/99845-selecting-from-two-tables/ Share on other sites More sharing options...
wildteen88 Posted April 6, 2008 Share Posted April 6, 2008 Look into using JOIN's Quote Link to comment https://forums.phpfreaks.com/topic/99845-selecting-from-two-tables/#findComment-510653 Share on other sites More sharing options...
Cyberspace Posted April 6, 2008 Author Share Posted April 6, 2008 I tried the following code, but it doesnt display the forename and surname. <?php $WalkNo = $_GET['WalkNo']; $query = "SELECT * FROM walker WHERE WalkNo='$WalkNo'"; $result = mysql_query($query); if(!$result){ // check if is something wrong print "Error"; }else{ if(mysql_num_rows($result) == 0){ print "No Participants exist"; }else{ while ($account = @mysql_fetch_array($result)) { $query = "SELECT * FROM members WHERE MemberRef='$MemberRef'"; $WalkNo=$account["WalkNo"]; $MemberRef=$account["MemberRef"] ?> <table class="join" cellspacing="0" width="861"> <tr> <td class="Header">WalkNo</td> <td class="Header">Member Ref</td> <td class="Header">Forename</td> <td class="Header">Surname</td> <td class="Header">DateJoined</td> </tr> <tr> <td class="Body"><?php echo $account["WalkNo"]; ?></td> <td class="Body"><?php echo $account["MemberRef"]; ?></td> <td class="Body"><?php echo $account["Forename"]; ?></td> <td class="Body"><?php echo $account["Surname"]; ?></td> <td class="Body"><?php echo $account["DateJoined"]; ?></td> </tr> <br> </table> <?php } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/99845-selecting-from-two-tables/#findComment-510715 Share on other sites More sharing options...
zenag Posted April 7, 2008 Share Posted April 7, 2008 assign $MemberRef=$account["MemberRef"] before $query = "SELECT * FROM members WHERE MemberRef='$MemberRef'"; like this..... $MemberRef=$account["MemberRef"]; $query = "SELECT * FROM members WHERE MemberRef='$MemberRef'"; $WalkNo=$account["WalkNo"]; ?> <table class="join" cellspacing="0" width="861"> <tr> Quote Link to comment https://forums.phpfreaks.com/topic/99845-selecting-from-two-tables/#findComment-511008 Share on other sites More sharing options...
Cyberspace Posted April 7, 2008 Author Share Posted April 7, 2008 assign $MemberRef=$account["MemberRef"] before $query = "SELECT * FROM members WHERE MemberRef='$MemberRef'"; like this..... $MemberRef=$account["MemberRef"]; $query = "SELECT * FROM members WHERE MemberRef='$MemberRef'"; $WalkNo=$account["WalkNo"]; ?> <table class="join" cellspacing="0" width="861"> Hey thanks for the reply: I have the following code but it still wont display the surname and forename from the Members table: <?php $WalkNo = $_GET['WalkNo']; $query = "SELECT * FROM walker WHERE WalkNo='$WalkNo'"; $result = mysql_query($query); if(!$result){ // check if is something wrong print "Error"; }else{ if(mysql_num_rows($result) == 0){ print "No Participants exist"; }else{ while ($account = @mysql_fetch_array($result)) { $MemberRef=$account["MemberRef"]; $query = "SELECT * FROM members WHERE MemberRef='$MemberRef'"; $WalkNo=$account["WalkNo"]; ?> <table class="join" cellspacing="0" width="861"> <tr> <td class="Header">WalkNo</td> <td class="Header">Member Ref</td> <td class="Header">Forename</td> <td class="Header">Surname</td> <td class="Header">DateJoined</td> </tr> <tr> <td class="Body"><?php echo $account["WalkNo"]; ?></td> <td class="Body"><?php echo $account["MemberRef"]; ?></td> <td class="Body"><?php echo $account["Forename"]; ?></td> <td class="Body"><?php echo $account["Surname"]; ?></td> <td class="Body"><?php echo $account["DateJoined"]; ?></td> </tr> <br> </table> <?php } } } ?> <tr> Quote Link to comment https://forums.phpfreaks.com/topic/99845-selecting-from-two-tables/#findComment-511332 Share on other sites More sharing options...
wildteen88 Posted April 7, 2008 Share Posted April 7, 2008 As I said earlier it'll be easier to use JOIN's. With a JOIN you can query multiple's tables at once. Can you post your table structure for the members and walkers table here and tell me what fields you want to retrieve from the two tables. EDIT: Try the following code instead. I had a guess at it: <?php $WalkNo = $_GET['WalkNo']; $query = "SELECT w.WalkerNo, m.MemberRef, m.Forname, m.Surename, m.DateJoined FROM members m, walker w WHERE m.MemberRef=w.MemberRef AND w.WalkerNo='$WalkNo'"; $result = mysql_query($query) or die('Query error!<br />Error:' . mysql_error() . 'Query: <pre>' . $query . '</pre>'); if(mysql_num_rows($result) == 0) { print "No Participants exist"; } else { ?> <table class="join" cellspacing="0" width="861"> <tr> <td class="Header">WalkNo</td> <td class="Header">Member Ref</td> <td class="Header">Forename</td> <td class="Header">Surname</td> <td class="Header">DateJoined</td> </tr> <?php while ($account = @ysql_fetch_assoc($result)) { ?> <tr> <td class="Body"><?php echo $account["WalkNo"]; ?></td> <td class="Body"><?php echo $account["MemberRef"]; ?></td> <td class="Body"><?php echo $account["Forename"]; ?></td> <td class="Body"><?php echo $account["Surname"]; ?></td> <td class="Body"><?php echo $account["DateJoined"]; ?></td> </tr> <?php } ?> </table> <?php } ?> Quote Link to comment https://forums.phpfreaks.com/topic/99845-selecting-from-two-tables/#findComment-511348 Share on other sites More sharing options...
Cyberspace Posted April 7, 2008 Author Share Posted April 7, 2008 As I said earlier it'll be easier to use JOIN's. With a JOIN you can query multiple's tables at once. Can you post your table structure for the members and walkers table here and tell me what fields you want to retrieve from the two tables. EDIT: Try the following code instead. I had a guess at it: <?php $WalkNo = $_GET['WalkNo']; $query = "SELECT w.WalkerNo, m.MemberRef, m.Forname, m.Surename, m.DateJoined FROM members m, walker w WHERE m.MemberRef=w.MemberRef AND w.WalkerNo='$WalkNo'"; $result = mysql_query($query) or die('Query error!<br />Error:' . mysql_error() . 'Query: <pre>' . $query . '</pre>'); if(mysql_num_rows($result) == 0) { print "No Participants exist"; } else { ?> <table class="join" cellspacing="0" width="861"> <tr> <td class="Header">WalkNo</td> <td class="Header">Member Ref</td> <td class="Header">Forename</td> <td class="Header">Surname</td> <td class="Header">DateJoined</td> </tr> <?php while ($account = @ysql_fetch_assoc($result)) { ?> <tr> <td class="Body"><?php echo $account["WalkNo"]; ?></td> <td class="Body"><?php echo $account["MemberRef"]; ?></td> <td class="Body"><?php echo $account["Forename"]; ?></td> <td class="Body"><?php echo $account["Surname"]; ?></td> <td class="Body"><?php echo $account["DateJoined"]; ?></td> </tr> <?php } ?> </table> <?php } ?> Hey thanks for the reply Table "members" has: MemberRef, Forename, Surname, Address1, Address2, County, Postcode, TelNo, MobNo,WorkNo, Email Table "walker" has: WalkNo, MemberRef, DateJoined Quote Link to comment https://forums.phpfreaks.com/topic/99845-selecting-from-two-tables/#findComment-511374 Share on other sites More sharing options...
wildteen88 Posted April 7, 2008 Share Posted April 7, 2008 I was close just needed to tweak the query. try: <?php $WalkNo = $_GET['WalkNo']; $query = "SELECT w.WalkerNo, m.MemberRef, m.Forname, m.Surename, w.DateJoined FROM members m, walker w WHERE w.MemberRef=m.MemberRef AND w.WalkerNo='$WalkNo'"; $result = mysql_query($query) or die('Query error!<br />Error:' . mysql_error() . 'Query: <pre>' . $query . '</pre>'); if(mysql_num_rows($result) == 0) { print "No Participants exist"; } else { ?> <table class="join" cellspacing="0" width="861"> <tr> <td class="Header">WalkNo</td> <td class="Header">Member Ref</td> <td class="Header">Forename</td> <td class="Header">Surname</td> <td class="Header">DateJoined</td> </tr> <?php while ($account = @mysql_fetch_assoc($result)) { ?> <tr> <td class="Body"><?php echo $account["WalkNo"]; ?></td> <td class="Body"><?php echo $account["MemberRef"]; ?></td> <td class="Body"><?php echo $account["Forename"]; ?></td> <td class="Body"><?php echo $account["Surname"]; ?></td> <td class="Body"><?php echo $account["DateJoined"]; ?></td> </tr> <?php } ?> </table> <?php } ?> Quote Link to comment https://forums.phpfreaks.com/topic/99845-selecting-from-two-tables/#findComment-511388 Share on other sites More sharing options...
Cyberspace Posted April 7, 2008 Author Share Posted April 7, 2008 I was close just needed to tweak the query. try: <?php $WalkNo = $_GET['WalkNo']; $query = "SELECT w.WalkerNo, m.MemberRef, m.Forname, m.Surename, w.DateJoined FROM members m, walker w WHERE w.MemberRef=m.MemberRef AND w.WalkerNo='$WalkNo'"; $result = mysql_query($query) or die('Query error!<br />Error:' . mysql_error() . 'Query: <pre>' . $query . '</pre>'); if(mysql_num_rows($result) == 0) { print "No Participants exist"; } else { ?> <table class="join" cellspacing="0" width="861"> <tr> <td class="Header">WalkNo</td> <td class="Header">Member Ref</td> <td class="Header">Forename</td> <td class="Header">Surname</td> <td class="Header">DateJoined</td> </tr> <?php while ($account = @mysql_fetch_assoc($result)) { ?> <tr> <td class="Body"><?php echo $account["WalkNo"]; ?></td> <td class="Body"><?php echo $account["MemberRef"]; ?></td> <td class="Body"><?php echo $account["Forename"]; ?></td> <td class="Body"><?php echo $account["Surname"]; ?></td> <td class="Body"><?php echo $account["DateJoined"]; ?></td> </tr> <?php } ?> </table> <?php } ?> wildteen88 that worked fine, thankyou so much Quote Link to comment https://forums.phpfreaks.com/topic/99845-selecting-from-two-tables/#findComment-511397 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.