PHP Sessions

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
PHP.Guy
Forum Newbie
Posts: 1
Joined: Sun May 25, 2008 11:28 am

PHP Sessions

Post 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!
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Re: PHP Sessions

Post 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'];
User avatar
Jaxolotl
Forum Contributor
Posts: 137
Joined: Mon Nov 13, 2006 4:19 am
Location: Argentina and Italy

Re: PHP Sessions

Post 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.
dbemowsk
Forum Commoner
Posts: 82
Joined: Wed May 14, 2008 10:30 pm

Re: PHP Sessions

Post 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.
User avatar
Jaxolotl
Forum Contributor
Posts: 137
Joined: Mon Nov 13, 2006 4:19 am
Location: Argentina and Italy

Re: PHP Sessions

Post 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'
      )
 
User avatar
deeessay
Forum Commoner
Posts: 55
Joined: Sat May 24, 2008 1:02 am

Re: PHP Sessions

Post 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?
dbemowsk
Forum Commoner
Posts: 82
Joined: Wed May 14, 2008 10:30 pm

Re: PHP Sessions

Post by dbemowsk »

My bad, you are correct. I for whatever reason was thinking mysql_fetch_row when I saw mysql_fetch_array.
Post Reply