<?php
$sql = mysql_query("SELECT userid, score FROM ranking ORDER BY id desc LIMIT 10"); // finding the top 10 ranked users
$query = $sql or die(mysql_error());
while($fetch = mysql_fetch_array($query)) {
$user_sql = mysql_query("SELECT user_name FROM users WHERE userid='".$fetch['userid']."'"); // finding the username by echoing the userid (that I'm getting from the other table - ranking.
$user_query = $user_sql or die(mysql_error());
$user_fetch = mysql_fetch_array($user_query);
?>
<tr>
<td>» <a target="_blank" href="profile.php?user=<?php echo $fetch['userid'];?>"><?php echo $user_fetch['user_name']; ?></a></td>
</tr>
<?php } ?>
<?php
$sql = mysql_query("SELECT userid, score FROM ranking ORDER BY id desc LIMIT 10"); // finding the top 10 ranked users
$query = $sql or die(mysql_error());
while($fetch = mysql_fetch_array($query)) {
$user_sql = mysql_query("SELECT user_name FROM users WHERE userid='".$fetch['userid']."'"); // finding the username by echoing the userid (that I'm getting from the other table - ranking.
$user_query = $user_sql or die(mysql_error());
$user_fetch = mysql_fetch_array($user_query);
?>
<tr>
<td>» <a target="_blank" href="profile.php?user=<?php echo $fetch['userid'];?>"><?php echo $user_fetch['user_name']; ?></a></td>
</tr>
<?php } ?>
You can combine those queries into 1 using a JOIN. It will make your life easier.
<?php
$sql = mysql_query("SELECT userid, score FROM ranking ORDER BY id desc LIMIT 10"); // finding the top 10 ranked users
$query = $sql or die(mysql_error());
while($fetch = mysql_fetch_array($query)) {
$user_sql = mysql_query("SELECT user_name FROM users WHERE userid='".$fetch['userid']."'"); // finding the username by echoing the userid (that I'm getting from the other table - ranking.
$user_query = $user_sql or die(mysql_error());
$user_fetch = mysql_fetch_array($user_query);
?>
<tr>
<td>» <a target="_blank" href="profile.php?user=<?php echo $fetch['userid'];?>"><?php echo $user_fetch['user_name']; ?></a></td>
</tr>
<?php } ?>
You can combine those queries into 1 using a JOIN. It will make your life easier.
<?php
$sql = mysql_query("SELECT userid, score FROM ranking ORDER BY id desc LIMIT 10"); // finding the top 10 ranked users
$query = $sql or die(mysql_error());
while($fetch = mysql_fetch_array($query)) {
$user_sql = mysql_query("SELECT user_name FROM users WHERE userid='".$fetch['userid']."'"); // finding the username by echoing the userid (that I'm getting from the other table - ranking.
$user_query = $user_sql or die(mysql_error());
$user_fetch = mysql_fetch_array($user_query);
?>
<tr>
<td>» <a target="_blank" href="profile.php?user=<?php echo $fetch['userid'];?>"><?php echo $user_fetch['user_name']; ?></a></td>
</tr>
<?php } ?>
SELECT forum.userid, forum_thread.name, users.user_name
FROM forum
JOIN forum_thread, users ON forum.category_id = forum_thread.post_category AND forum.userid = users.user_id
ORDER BY forum_thread.name DESC LIMIT 10
Your example has a redundant JOIN in there. The comma (,) effectively acts like a JOIN.
To match across databases (schemas), you simply prepend the database name to the table. As with same-schema JOINs, there has to be a common field between the two database.tables to JOIN on. So, to expand your example: