PHP issue, calling users ID twice

Status
Not open for further replies.

Hender

King Tinkerer
Mar 3, 2016
304
122
Hello, so in my RP, peoples stats are in rp_stats table.
Their username, look etc is stores in the users table.

So in my code, it calls information from users perfectly.
but it doesn't call information from rp_stats if i use the same ".$_SESSION['user']['id']." function.
If i change it to an ID, it calls the information.

I just can't use the ".$_SESSION['user']['id']." function twice.

What can i do to fix this?

PHP:
<?php
$getuser = mysql_query("SELECT * FROM users WHERE id = '".$_SESSION['user']['id']."'");
$stats = mysql_fetch_array($getuser);
$credits = $stats['credits'];
$look = $stats['look'];
$motto = $stats['motto'];


$getuserstats = mysql_query("SELECT * FROM rp_stats WHERE id = '".$_SESSION['user']['id']."'");
$userstats = mysql_fetch_array($getuserstats);
$bank = $userstats['bank'];
 

Hender

King Tinkerer
Mar 3, 2016
304
122
Have you tried the query with a hard coded id, to verify your hypothesis that the issue is truly with the session variable? Because I'm quite confident that that is not the issue.
Yes if i put an ID in it does call the stats from that ID.
 

JayC

Always Learning
Aug 8, 2013
5,493
1,398
Try This:
Code:
<?php
$getuser = mysql_query("SELECT * FROM users WHERE id = '".$_SESSION['user']['id']."'");
if(mysql_num_rows($getuser) > 0){
$stats = mysql_fetch_assoc($getuser);
$credits = $stats['credits'];
$look = $stats['look'];
$motto = $stats['motto'];

$getuserstats = mysql_query("SELECT * FROM `rp_stats` WHERE id = '".$stats['id']."' LIMIT 1");
if(mysql_num_rows($getuserstats) > 0){
$userstats = mysql_fetch_assoc($getuserstats);
$bank = $userstats['bank'];
}
}
?>
 

Hender

King Tinkerer
Mar 3, 2016
304
122
Try This:
Code:
<?php
$getuser = mysql_query("SELECT * FROM users WHERE id = '".$_SESSION['user']['id']."'");
if(mysql_num_rows($getuser) > 0){
$stats = mysql_fetch_assoc($getuser);
$credits = $stats['credits'];
$look = $stats['look'];
$motto = $stats['motto'];

$getuserstats = mysql_query("SELECT * FROM `rp_stats` WHERE id = '".$stats['id']."' LIMIT 1");
if(mysql_num_rows($getuserstats) > 0){
$userstats = mysql_fetch_assoc($getuserstats);
$bank = $userstats['bank'];
}
}
?>


Doesn't return anything, its been winding me up all day.
 

JayC

Always Learning
Aug 8, 2013
5,493
1,398
Doesn't return anything, its been winding me up all day.
Try this and tell me what happens:
Code:
<?php
$findUser = mysql_fetch_assoc(mysql_query("SELECT * FROM users WHERE username = '".$_SESSION['user']['username']."' LIMIT 1 "));
echo $findUser['username'];
echo $findUser['id'];
echo $findUser['credits'];
echo $findUser['motto'];
?>
 

Hender

King Tinkerer
Mar 3, 2016
304
122
So
Try this and tell me what happens:
Code:
<?php
$findUser = mysql_fetch_assoc(mysql_query("SELECT * FROM users WHERE username = '".$_SESSION['user']['username']."' LIMIT 1 "));
echo $findUser['username'];
echo $findUser['id'];
echo $findUser['credits'];
echo $findUser['motto'];
?>
yeah, that does call information from the users table.


But see, if i changed that to get information from the rp_stats using this for example:
PHP:
$findStats = mysql_fetch_assoc(mysql_query("SELECT * FROM rp_stats WHERE id = '".$_SESSION['user']['id']."' LIMIT 1 "));
echo $findStats['bank'];
It doesn't work.

So why can I call information from users using the session id, but not RP_users? boggled my mind.
 

JayC

Always Learning
Aug 8, 2013
5,493
1,398
So

yeah, that does call information from the users table.


But see, if i changed that to get information from the rp_stats using this for example:
PHP:
$findStats = mysql_fetch_assoc(mysql_query("SELECT * FROM rp_stats WHERE id = '".$_SESSION['user']['id']."' LIMIT 1 "));
echo $findStats['bank'];
It doesn't work.

So why can I call information from users using the session id, but not RP_users? boggled my mind.
Try this:
Code:
<?php
$findStats = mysql_fetch_assoc(mysql_query("SELECT * FROM `rp_stats` WHERE `id` = '".$_SESSION['user']['id']."' LIMIT 1 "));
echo $findStats['bank'];
?>
 

BIOS

ಠ‿ಠ
Apr 25, 2012
906
247
You don't need to use it twice anyway, you can do this with one query.

PHP:
<?php

$id = mysql_real_escape_string($_SESSION['user']['id']);
$getUser = mysql_query("
    SELECT
        u.*, s.*
    FROM
        users u
    LEFT JOIN
        rp_stats s
    ON
        u.id = s.id
    WHERE
        u.id = '".$id."'
");
$data = mysql_fetch_array($getUser);

echo $data['motto'];
echo $data['bank'];

?>

Let me know the result as I haven't tested this and just quickly wrote it up.
 

Hender

King Tinkerer
Mar 3, 2016
304
122
You don't need to use it twice anyway, you can do this with one query.

PHP:
<?php

$id = mysql_real_escape_string($_SESSION['user']['id']);
$getUser = mysql_query("
    SELECT
        u.*, s.*
    FROM
        users u
    LEFT JOIN
        rp_stats s
    ON
        u.id = s.id
    WHERE
        u.id = '".$id."'
");
$data = mysql_fetch_array($getUser);

echo $data['motto'];
echo $data['bank'];

?>

Let me know the result as I haven't tested this and just quickly wrote it up.

This grabs the motto from users but doesn't grab bank from rp_stats.
 

Hender

King Tinkerer
Mar 3, 2016
304
122
Is there a row in rp_stats with your user's ID as the id?

var_dump($data); to see what data it has

Cant believe that i forgot to add on register to insert the players stats into rp_stats, no wonder its not showing any info.
hahah omfg i'm sorry, thanks for reminding me.
 

BIOS

ಠ‿ಠ
Apr 25, 2012
906
247
Cant believe that i forgot to add on register to insert the players stats into rp_stats, no wonder its not showing any info.
hahah omfg i'm sorry, thanks for reminding me.
So the script is working after you added the row for your user in stats?
 
Status
Not open for further replies.

Users who are viewing this thread

Top