Chris.P Posted May 15, 2007 Share Posted May 15, 2007 I have a query below that gets its results and puts them in a table. The current results are from two fields in a db table. I am wondering if it is possible to create a second query to get results from a different db table and echo them out in the same html table as the ones already there? Many thanks. <?php include_once 'functions.php'; loginDetails(); session_start(); $query = "SELECT * FROM users ORDER BY id"; // execute query $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); // see if any rows were returned if (mysql_num_rows($result) > 0) { // yes // print them one after another echo "<table id=drtable>"; echo "<tr><td><h2>Band ID</h2></td>"; echo "<td><h2>Band Name</h2></td></tr>"; while($row = mysql_fetch_assoc($result)) { echo "<tr>"; echo "<td><h2><a href=profile.php?bandid=".$row['id'].">".$row['id']."</a></h2></td>"; echo "<td><h2><a href=profile.php?bandid=".$row['id'].">".$row['bandname']."</a></h2></td>"; echo "</tr>"; } echo "</table>"; } else { // no // print status message echo "No bands found."; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/51500-results-from-two-db-tables-into-one-html-table/ Share on other sites More sharing options...
suttercain Posted May 15, 2007 Share Posted May 15, 2007 Yes this is possible. See MySQL Join or you can just run another query and do something like: while($row2 = mysql_fetch_assoc($result2) { echo $row2['item']; } Both of those ways will work. Quote Link to comment https://forums.phpfreaks.com/topic/51500-results-from-two-db-tables-into-one-html-table/#findComment-253626 Share on other sites More sharing options...
Chris.P Posted May 15, 2007 Author Share Posted May 15, 2007 Cheers I did it with a MySQL join. It seems to be working although images are not showing properly as the image locations are all getting %3c/a added on after the file extension. Has me baffled as to why this is happening, any ideas? Full query with output <?php include_once 'functions.php'; loginDetails(); session_start(); $query = "SELECT users.id, users.bandname, images.path ". "FROM users, images ". "WHERE users.id = images.id"; //$query = "SELECT * FROM users ORDER BY id"; // execute query $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); // see if any rows were returned if (mysql_num_rows($result) > 0) { // yes // print them one after another echo "<table id=drtable>"; echo "<tr><td><h2>Band Image</h2></td>"; echo "<td><h2>Band Name</h2></td>"; echo "<td><h2>Band ID</h2></td></tr>"; while($row = mysql_fetch_assoc($result)) { echo "<tr>"; echo "<td><h2><a href=profile.php?bandid=".$row['id'].">","<img src=".$row['path']."</a></h2></td>"; echo "<td><h2><a href=profile.php?bandid=".$row['id'].">".$row['bandname']."</a></h2></td>"; echo "<td><h2><a href=profile.php?bandid=".$row['id'].">".$row['id']."</a></h2></td>"; echo "</tr>"; } echo "</table>"; } else { // no // print status message echo "No bands found."; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/51500-results-from-two-db-tables-into-one-html-table/#findComment-254034 Share on other sites More sharing options...
suttercain Posted May 16, 2007 Share Posted May 16, 2007 Try this <?php echo "<td><h2><a href=profile.php?bandid='".$row['id']."'><img src='".$row['path']."'></a></h2></td>"; You didn't have quotes around your img path and also a closing ">" tag. Let me know if that works. Quote Link to comment https://forums.phpfreaks.com/topic/51500-results-from-two-db-tables-into-one-html-table/#findComment-254612 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.