Page 1 of 1

Array question

Posted: Fri Apr 28, 2006 11:21 am
by spacebiscuit
Hi,

I want to return an SQL query result into a session variable here is my code:

Code: Select all

$query = "SELECT * FROM table1";

$result = mysql_query($query);
            
$_SESSION[temp] = mysql_fetch_row($result);
All appears will until I try and access the data. I have tried this:

Code: Select all

echo"$_SESSION[temp][0]
and....

Code: Select all

echo"$_SESSION[temp]
But it does not work, I know the result is written to an array but is this possible with a session variable?

Thanks,

Rob.

Posted: Fri Apr 28, 2006 11:35 am
by hawleyjr
First you really need to use a single or double quote with your session var:

Code: Select all

$_SESSION['temp']
Then, try using print_r() to see what your session var is holding...

Posted: Fri Apr 28, 2006 3:59 pm
by spacebiscuit
Thanks for the reply, here is the output:

Code: Select all

print_r($_SESSION['temp']

Array ( [0] => 1 [1] => 74995 [2] => N/A [3] => Lock [4] )
So the data is there but how do I access the indexes correctly?

Rob.

Posted: Fri Apr 28, 2006 4:17 pm
by feyd

Code: Select all

$_SESSION['temp'][0]
$_SESSION['temp'][1]
$_SESSION['temp'][2]

Posted: Fri Apr 28, 2006 6:56 pm
by Ollie Saunders
Use mysql_fetch_assoc() and use associative indices

Code: Select all

$_SESSION['temp']['columnName']
$_SESSION['temp']['columnName']
$_SESSION['temp']['columnName']
First you really need to use a single or double quote with your session var:
Yes. This will attempt to read a value from a constant named temp

Code: Select all

$_SESSION[temp];
Whilst this will actually use the index called temp:

Code: Select all

$_SESSION['temp'];
That first example should generate a warning unless you have defined a constant called temp

Code: Select all

define('temp','value for constant');
In which case you should be sticking to the naming convention and naming the constant in UPPERCASE, so TEMP