Trying to grab from two db tables to display them with php

WanknessHD

Hardcore Habboer
Jun 13, 2011
48
5
I have a stats page that displays credits/duckets/diamonds/room visits/repects given/recieved/online time etc.. only problem I have is I'm not very educated with php and I dont know how to grab from two tables in my database. I have my credits and duckets and diamonds displayed perfectly, but the rest of them display the amount but not the user associated with them.

571f02290b994d289615ccf963569753.png


The code to display my credits etc:
PHP:
<?php
                                                                $getstats = mysql_query("SELECT * FROM users ORDER BY vip_points DESC LIMIT 6");
                                                                $oe = 1;
                                                                while ($info = mysql_fetch_array($getstats))
                                                                {
                                                                if ($oe == 2)
                                                                {
                                $oe = 1;
                                                                }
                                                                else
                                                                {
                                $oe = 2;
                                                                }   
                                                                echo '<table width="100%" style="padding: 5px; background-color: ' . (($oe == 2) ? '#fff' : '') . ';">
                                                                <tbody>
                                                                <tr>
                                                                <td valign="middle" width="25"><img style="margin-top: -10px;" src="http://www.habbo.nl/habbo-imaging/avatarimage?figure=' .$info['look'] . '&direction=3&headonly=1"></td>
                                                                <td valign="top">
                                                                <p style="font-size: 90%;"><strong><a ' .$info['username'] . '">' .$info['username'] . '</a></strong><br></i>' . $info['vip_points'] . '</i></p>
                                                                </td></tr></tbody></table>';
                                                                }
                                                                ?>


And the code I was using to grab the others (same code just tweaked to show what I have)
PHP:
<?php
                                                                $getstats = mysql_query("SELECT * FROM user_stats ORDER BY RoomVisits DESC LIMIT 6");
                                                                $oe = 1;
                                                                while ($info = mysql_fetch_array($getstats))
                                                                {
                                                                if ($oe == 2)
                                                                {
                                $oe = 1;
                                                                }
                                                                else
                                                                {
                                $oe = 2;
                                                                }   
                                                                echo '<table width="100%" style="padding: 5px; background-color: ' . (($oe == 2) ? '#fff' : '') . ';">
                                                                <tbody>
                                                                <tr>
                                                                <td valign="middle" width="25"><img style="margin-top: -10px;" src="http://www.habbo.nl/habbo-imaging/avatarimage?figure=' .$info['look'] . '&direction=3&headonly=1"></td>
                                                                <td valign="top">
                                                                <p style="font-size: 90%;"><strong><a ' .$info['username'] . '">' .$info['username'] . '</a></strong><br></i>' . $info['RoomVisits'] . '</i></p>
                                                                </td></tr></tbody></table>';
                                                                }
                                                                ?>

If anyone is willing to guide me on how to fix this im more than down, not looking for handouts as I would like to know how to fix something like this in the future, so if someone could maybe explain and help thatd be cool. Thanks for your time in advance.


Edit: Im able to call the first three because theyre in the users table associated with the character, for the rest im trying to grab the character so it can be displayed like the others.
 

Core

Member
Nov 10, 2016
356
138
[pre]SELECT
user.username, user.look, user.motto, stat.Respect
FROM
users user, user_stats stat
WHERE
user.id = stat.user_id
ORDER BY
stat.Respect DESC
LIMIT 6[/pre]

You could also do

[pre]user.id = stat.user_id AND user.rank < 4[/pre]

Which would only show stats for users.


or using a joint query... (cba highlighting it lol) The second method is the better method.

SELECT user.username, stat.Respect FROM users user JOIN user_stats stat ON user.id = stat.user_id

I have made user_id bold because not 100% sure on the column name, my plus doesn't use the default user_stats column.
 

WanknessHD

Hardcore Habboer
Jun 13, 2011
48
5
[pre]SELECT
user.username, user.look, user.motto, stat.Respect
FROM
users user, user_stats stat
WHERE
user.id = stat.user_id
ORDER BY
stat.Respect DESC
LIMIT 6[/pre]

You could also do

[pre]user.id = stat.user_id AND user.rank < 4[/pre]

Which would only show stats for users.


or using a joint query... (cba highlighting it lol) The second method is the better method.

SELECT user.username, stat.Respect FROM users user JOIN user_stats stat ON user.id = stat.user_id

I have made user_id bold because not 100% sure on the column name, my plus doesn't use the default user_stats column.

PHP:
 $getstats = mysql_query("SELECT user.username, user.look, stat.Respect FROM users user, user_stats Respect WHERE user.id = stat.id ORDER BY stat.Respect DESC LIMIT 6");

So I did this and it did not display anything, I may not be understanding correctly. What is the user.etc for? Im trying to tweak it see if anything appears.

PHP:
$getstats = mysql_query("SELECT users.username, users.look, user_stats.Respect FROM users, user_stats WHERE user_stats.id = users.username ORDER BY user_stats.Respect DESC LIMIT 6");
Tried this as well.
 

Core

Member
Nov 10, 2016
356
138
PHP:
 $getstats = mysql_query("SELECT user.username, user.look, stat.Respect FROM users user, user_stats Respect WHERE user.id = stat.id ORDER BY stat.Respect DESC LIMIT 6");

So I did this and it did not display anything, I may not be understanding correctly. What is the user.etc for? Im trying to tweak it see if anything appears.

PHP:
$getstats = mysql_query("SELECT users.username, users.look, user_stats.Respect FROM users, user_stats WHERE user_stats.id = users.username ORDER BY user_stats.Respect DESC LIMIT 6");
Tried this as well.

Use or die(mysql_error()); to see if you get any errors.
 

WanknessHD

Hardcore Habboer
Jun 13, 2011
48
5
Use or die(mysql_error()); to see if you get any errors.
Broken page, it just doesn't load.
 
Got it to work @Core did this.
PHP:
<?php
                                                                $getstats = mysql_query("SELECT * FROM user_stats ORDER BY RoomVisits DESC LIMIT 5") or die(mysql_error());
                                                                $oe = 1;
                                                                while ($info = mysql_fetch_array($getstats))
                                                                {                                                                   
                                                                $userInfo = mysql_fetch_array(mysql_query("SELECT `username`,`look` FROM users WHERE `id` = '".$info['id']."'"));
                                                                {
                                                                if ($oe == 2)
                                                                {
                                                                $oe = 1;
                                                                }
                                                                else
                                                                {
                                                                $oe = 2;
                                                                }     
                                                                echo '<table width="100%" style="padding: 5px; background-color: ' . (($oe == 2) ? '#fff' : '') . ';">
                                                                <tbody>
                                                                <tr>
                                                                <td valign="middle" width="25"><img style="margin-top: -10px;" src="http://www.habbo.nl/habbo-imaging/avatarimage?figure=' .$userInfo['look'] . '&direction=3&headonly=1"></td>
                                                                <td valign="top">
                                                                <p style="font-size: 90%;"><strong><a style="text-decoration: none" href="{url}/home/' .$userInfo['username'] . '"">' .$userInfo['username'] . '</a></strong><br></i>' . $info['RoomVisits'] . '</i></p>
                                                                </td></tr></tbody></table>';
                                                                }
                                                                }
                                                                ?>

Only issue I have is no matter how many I want to display it only shows 5, so for the mean time I just changed them all to 5 until I can get that fixed.
 

Users who are viewing this thread

Top