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

Graeme

Recommended Answers

All 2 Replies

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

Hi

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.

Graeme

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.