Small PHP/mySQL Syntax Problem

Associate
Joined
21 Sep 2005
Posts
180
Location
Dundee
Well, no hair left. I've tried this just about every which way (it feels like) but i just know it's something really, really stupid that i'm missing.

Underneath is a query. I've tested that there is at least one result row in the query so that seems to be ok. However, when i try to assign the result to a Session variable - $_SESSION['TimeSoFar'] -, it just doesn't seem to happen.

PHP:
//get the time so far for that lessonid
	$queryTimeAtStart = "SELECT '$_SESSION[LessonID]' FROM UserData
					WHERE User_ID = '$_SESSION[User_ID]'
					";
	$resultTimeAtStart = mysql_query ($queryTimeAtStart);				
	$rowTimeAtStart = mysql_fetch_array ($resultTimeAtStart) ;

	$num_rows = mysql_num_rows($resultTimeAtStart);
	
	echo 'This is the number of results - '.$num_rows.'<br />';
	
	$_SESSION['TimeSoFar'] = $rowTimeAtStart[$_SESSION['LessonID']];
I believe the offending bit is this...
PHP:
$_SESSION['TimeSoFar'] = $rowTimeAtStart[$_SESSION['LessonID']];

All this does is give it the value of $_Session['LessonID'], rather than the result of the query. Could someone please put me right? Feel free to mock if it helps. :)
 
So you're trying to assign a value of a column called "$_SESSION[LessonID]" to "$_SESSION['TimeSoFar']" ?
 
Yup, basically.

'$_SESSION[LessonID]' - These are basically a range of columns which all have a number as their column name. I can run what appears to be the same query in PHPMYADMIN, with values changed for relevant ones, and get the right result, but only on a column i have changed from '1' to 'Lesson1'. If i run it with a column name that is a number, it just gives me the column number itself back.

So, thinking i would change the column names and all would be good, i first tested it again from the script but first making $_SESSION[LessonID] equal something with the syntax of 'Lesson1', so giving the same format that was used in PHPMYADMIN with success. However, still doesn't want to work from the script.

Still stumped.
 
in you mysql query you've got

Code:
$queryTimeAtStart = "SELECT '$_SESSION[LessonID]' FROM UserData
                              WHERE User_ID = '$_SESSION[User_ID]'";

now the bit $_SESSION[LessonID] is wrong you've missed out your quotes so replace it with $_SESSION['LessonID'] otherwise it's not going to get the values out of that session variable. The same goes for $_SESSION[User_ID].

Also its not good to directly inject these variables into the sql statement without running some checks.
 
Last edited:
Pho - session_start() is in and working fine.

Gman - thanks, but that syntax works fine in every other query i use them in. Is there any specific reason why they would not work in this case? Also, those Session variables are generated from the database.

Keep 'em coming. :)
 
Lovemonk said:
Pho - session_start() is in and working fine.

Gman - thanks, but that syntax works fine in every other query i use them in. Is there any specific reason why they would not work in this case? Also, those Session variables are generated from the database.

Keep 'em coming. :)


if thats the case then all I can suggest is try echoing the line

Code:
$rowTimeAtStart[$_SESSION['LessonID']];

to make sure it is putting what you think into the session variable
 
Have changed all columns in database from just a number eg 1,2,3 etc to Lesson1, Lesson2, Lesson3 etc.

Have also modified query and can get it working... sometimes..

Modified query...can't stand those forum PHP tags...
Code:
$queryTimeAtStart = "SELECT $lessonID FROM UserData
				WHERE User_ID = '$_SESSION[User_ID]'
				";						
$resultTimeAtStart = mysql_query ($queryTimeAtStart);				
$rowTimeAtStart = mysql_fetch_array ($resultTimeAtStart) ;

$lessonID is generated like this...
Code:
$lessonID = 'Lesson'.$_SESSION['LessonID'];

However, the query fails but works if i hard code the $lessonID string like so..
Code:
$lessonID = 'Lesson1';

Then, if i hard code the $lessonID string like above, the following bit of code works great, as it should...
Code:
$_SESSION['TimeSoFar'] = $rowTimeAtStart['$_SESSION[LessonID]'];

Basically, it seems i'm going wrong with the dynamic generation of first the string that acts as the Select column, and then also passing the result into the Session variable. I'm about to scream.
 
as before does $_SESSION['LessonID'] realy contain what you think it contains ? try just echoing this session variable and see what you get. Becasue when you hardcode this part it works fine so $_SESSION['LessonID'] can't contain what you think it does.
 
Have managed to fix it this morning by cleaning up my concatenated string that goes into the query as the column name.

Code:
mysql_real_escape_string($lessonID);
Thanks for your help, I appreciate you taking the time.
 
Back
Top Bottom