Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pulling data off of sql table with php using sessions

I'm trying to pull data off my sql table with php unique to the session's ID, however I only get the position that the user is in when I echo anything out!

<?php
include 'core/init.php';
$user_info = $_SESSION['user_id'];
?>
<html>....
<h1><?php echo $user_info['firstname'];?>&nbsp<?php echo $user_info['firstname'];?> </h1>

displays as:

5 5

if I log in with the fifth position in the database!

like image 484
thedullmistro Avatar asked Oct 18 '12 19:10

thedullmistro


1 Answers

The reason why you are getting 5 is for this code:

<?php echo $user_info['firstname'];?>

is that $_SESSION['user_id'] 's value is 5. So after the assignment $user_info's value is 5. Now because the $_SESSION['user_id'] is not set to an array as your intention seems to be. The result is that $user_info is taken as a string and ['firstname'] evaluates to [0]. If you like, you can update the ID 5 to 54, etc. You will always get the first character of your ID.

To correct this, try changing the last 2 lines before the return in your user_data function to:

$data = mysql_fetch_assoc(mysql_query("SELECT $fields FROM `users` WHERE `user_id` = $user_id"));
$data = array_merge(array($user_id), $data);
return $data;

if (logged_in() === true) { 
    $user_data = user_data($_SESSION['user_id'], 'username', 'first_name', 'last_name',     'email');
    $user_data = user_data($session_user_id, 'user_id', 'username', 'first_name', 'last_name', 'email');
}

to:

if (logged_in() === true) { 
    $user_data = user_data($_SESSION['user_id'], 'username', 'first_name', 'last_name', 'email');
    $_SESSION['user_id'] = $user_data;
}
like image 109
Tash Pemhiwa Avatar answered Oct 19 '22 05:10

Tash Pemhiwa