Jump to content
Larry Ullman's Book Forums

Recommended Posts

See script 12.12 from the 4th edition.

This login.php script sets some session data.

on line 19-> $_SESSION['user_id'] = $data['user_id];

 

In another script I retrieve information in a query using a WHERE clause containing $_SESSION['user_id'].   But it does not work.

 

Here is some of the code:

 

</php script view students grades.

session_start();

require ('./mysqli_connect.php');

$q = "SELECT * FROM TranscriptView WHERE studentID = $_SESSION['user_id'];

$r = @mysql_query ($dbc, $q);

$num = mysqli_num_rows($r);

 

But the variable $num is not one.

The routine fails at this point

 

It is like the session variable is a string, not an integer!

 

Can anyone provide me with direction?

 

Wes Smith

 

Link to comment
Share on other sites

There are several potential issues here.

To start with, your query string is not being closed by a double quote at the end.

In addition, it's usually best to enclose all array and object values being output within a string with a set of curly brackets to keep the PHP interpreter from getting confused.

 

In other words, I would change your query to the following:

$q = "SELECT * FROM TranscriptView WHERE studentID = {$_SESSION['user_id']};";

Next, I would check that the $_SESSION['user_id'] value is indeed an integer. You can use the is_int function in PHP to check that (http://php.net/manual/en/function.is-int.php).

Lastly, I'd try running the following static query from phpMyAdmin or wherever to ensure that your query is valid to begin with:

SELECT * FROM TranscriptView WHERE studentID = 1;

Please let us know what you find.

Thanks.

Link to comment
Share on other sites

Here is more details about my problems using PHP sessions:

 

1  <?php # Script 10.1 - view_one_students_report_copy.php

2  // This script retrieves the grades for students.

3  session_();

4  $page_title = 'View Your Courses And Grades.';

5  // include ('includes/headerStuReport.html');

6  require ('./mysqli_connect.php');

7  echo "session number =".$_SESSION[user_id'];

8  // Define the query:

9  $q = "SELECT * FROM TranscriptView WHERE StudentID = {$_SESSION['user_id']}";

10 $r = @mysqli_query ($dbc, $q);

11

12 // Count the number of returned rows:

13 $num = mysqli_num_rows($r);

14

15 ?>

=====================================================

 

1st:  I run the query(line 9) in using PHP MYADM. There is no problem.

2nd: When I run the short code above, I get the following error:

 

Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ']' in /home/content/00/10035700/html/view_one_students_report_copy_copy.php on line 9

 

I don't understand what I am doing wrong.

Any help would be appreciated!

Wes Smith

wesmith4@gmail.com

 

Link to comment
Share on other sites

 Share

×
×
  • Create New...