Jump to content
xisto Community
Sign in to follow this  
suicide1405241470

Cross-Table Data Retrieval

Recommended Posts

I'm using the following to retrieve data from stat_table<?php/* Include Files *********************/session_start(); include("database.php");include("login.php");/*************************************/echo "<table border=1>";$query = "SELECT STAT1 FROM stat_table WHERE GAME_ID=1 ORDER BY STAT1 DESC";$result = mysql_query($query) or die(mysql_error()); //runs the query$p=1;while($row=mysql_fetch_row($result)){$i=0;while ($i < mysql_num_fields($result)){$field_name=mysql_fetch_field($result, $i);echo "<tr><td>$p</td>" . "<td>" . $row[$i] . "</td></tr>";$i++;$p++;}}echo "</table>";?>This displays the following:1 yki 2 fy I also have user ID stored within stat_table, which corresponds to the user_id stored within the table user_table. I'm very new to php/mysql and I can't get my head around doing the following:Getting the user_id along with STAT1 from stat_table and then using the user_id to extract user_name from user_table and put it alongside the relevent STAT1 in the table above (the yki, fy bit).

Share this post


Link to post
Share on other sites

Assuming mySQL syntax is SQL 92 compliant try a simple join:"SELECT STAT1 FROM stat_table WHERE GAME_ID=1 ORDER BY STAT1 DESC";SELECT a.stat1, a.userid, b.usernameFROM stat_table a, user_table bWHERE a.userid = b.userid AND a.game_id = 1ORDER BY 1 DESC;This should get you close. I am confused however, because your ORDER BY clause should produce2 fy1 ykiinstead of what is in your post.js :)

Share this post


Link to post
Share on other sites

That should work on MySQL. And the order is right. See the numbers are just values of the variable p which is incremented on every round of the loop. So "fy" and "yki" are the entries that the database orders.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.