Hi all

I have been looking at the following upside down and sideways for a day now.

I have a PHP page that is making use of a session value in an SQL query. The query requires an integer (it's searching on the numeric index of a table in the database). The session value is coming over as a string but is in the form 1 or 77 or 234 and so on.

The problem is that when I try to convert the session value to an integer it is always getting converted to a zero integer.

I have tried every combination of int, intval etc etc until I am blue in the face - any clues would be appreciated


6 Years
Discussion Span
Last Post by sakwoya

Do you have an example of the code?
Perhaps you could put up the actual error message and the print_r($_SESSION) output.



I've ditched the $_SESSION and gone with a $_GET which is working but I'd still be interested if anybody can come up with an explanation for the behaviour - I've butchered my original trial files but here's basically what I had (apologies for any syntax errors)

We set a PHP Session 'example_session' with a single numeric value (e.g. 12)

That session value is then called in another php file along the lines of

echo $_SESSION['example_session'];         //returns 12
echo gettype($_SESSION['example_session']);//returns string

$example = $_SESSION['example_session'];
echo $example;                            //returns 12
echo gettype($example);                   //returns string

$example = (int)$_SESSION['example_session'];
echo $example;                            //returns 0
echo gettype($example);                   //returns integer

$example = intval($_SESSION['example_session']);
echo $example;                            //returns 0
echo gettype($example);                   //returns integer

$example *= $_SESSION['example_session'];
echo $example;                            //returns 0
echo gettype($example);                   //returns integer

Later in the file there is a SQL query along the lines of:

"SELECT * FROM `table` WHERE table_id = $example"

This query will return empty as we are searching a field (table_id) that holds integer values with a search query of either a zero integer or a string.

I have no idea why I am getting a 0 (or NULL?) value when I try to convert the session value.


This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.