Page 1 of 1

PHP Sessions

Posted: Sun May 25, 2008 11:34 am
by PHP.Guy
Hey Everyone,

Im having trouble with my sessions - at least thats what I think - :(. I have
<?php session_start(); ?> at the top of my page...

Then Im using <?php echo $_SESSION['first_name'];?> to show the user's first name on the page and it works good.
Now Im trying to use, <?php echo $_SESSION['location'];?>

To show the users location but its not retrieving their location. And Im wondering if anyone might know why? Im retrieving the users first_name and displaying it on the page and it works ok, how come it wont retrieve their location?

If anyone knows why this is, any help would be great. What I did to try and fix this problem is approach it a different way and just used this script where I want their location:

Code: Select all

<?php
$con = mysql_connect("***","***","***") or die('Could not connect: ' . mysql_error());
mysql_select_db("login", $con);
 
$result = mysql_query("SELECT * FROM users");
while($row = mysql_fetch_array($result)){
  echo "{$row['location']}";
}
?>
But the problem with using that script is it isn't just listing the location for that user, its listing all the locations from the 'location' column in the database.

Any help would be great - thanks!

Re: PHP Sessions

Posted: Sun May 25, 2008 2:51 pm
by andym01480
Your code is selecting every user's details. If you want to just select the location of the user you will need to adjust the mysqlquery and add

Code: Select all

WHERE username='$username' LIMIT 1
- I don't know the column name or what variable holds the identifier for that user, so you will need to change username and $username. Then you can set

Code: Select all

$_SESSION['location']=$row['location'];

Re: PHP Sessions

Posted: Sun May 25, 2008 6:08 pm
by Jaxolotl
I would suggest you to use some unique index or primary key index to retrive user details on your WHERE condition of the query unless the username is itself a unique value of your user table.

Re: PHP Sessions

Posted: Sun May 25, 2008 7:01 pm
by dbemowsk
Your problem lies in your fetch.

Code: Select all

# while($row = mysql_fetch_array($result)){
#   echo "{$row['location']}";
With this you are using mysql_fetch_array which grabs the data into a numerically indexed array. You then try to retrieve the ['location'] which is an associative array. These are 2 different array types. If you want to use the associative array then you must use mysql_fetch_assoc.

Hope that answers your problem.

Re: PHP Sessions

Posted: Sun May 25, 2008 7:20 pm
by Jaxolotl
dbemowsk wrote:Your problem lies in your fetch.

Code: Select all

# while($row = mysql_fetch_array($result)){
#   echo "{$row['location']}";
With this you are using mysql_fetch_array which grabs the data into a numerically indexed array. You then try to retrieve the ['location'] which is an associative array. These are 2 different array types. If you want to use the associative array then you must use mysql_fetch_assoc.

Hope that answers your problem.
Wrong my friend mysql_fetch_array fetch BOTH numerical and associative

mysql_fetch_array — Fetch a result row as an associative array, a numeric array, or both
mysql_fetch_assoc — Fetch a result row as an associative array
mysql_fetch_row — Get a result row as an enumerated array


example
user table

Code: Select all

 
ID  |username |location |
1   |Paolo    |Italy    |
 
Fetch array
array('0' => '1',
      'ID' => '1',
      '1' => 'Paolo',
      'username' => 'Paolo',
      '2' => 'Italy',
      'location' => 'Italy'
      )
      
Fetch assoc
array('ID' => '1',
      'username' => 'Paolo',
      'location' => 'Italy'
      )
      
Fetch row
array('0' => '1',
      '1' => 'Paolo',
      '2' => 'Italy'
      )
 

Re: PHP Sessions

Posted: Sun May 25, 2008 8:07 pm
by deeessay
wait a minute I'm a little confused here... I thought you were having problems with the session variable?

i don't see any session in your code... and the query seems fine. could you post the part where you declared the session variables?

Re: PHP Sessions

Posted: Wed May 28, 2008 5:19 am
by dbemowsk
My bad, you are correct. I for whatever reason was thinking mysql_fetch_row when I saw mysql_fetch_array.