Sean Limpens Sean Limpens - 1 month ago 9
MySQL Question

New $_SESSION variable not created after query?

I'm trying to build a login process where, by using $_SESSION variables, the login credentials of the user are stored and used to show their relevant data from the database on screen (i.e. they will only see the school data that they work for).

<?php
session_start();

if(!isset($_SESSION['Initials'], $_SESSION['Surname']))
{

$host = "xxx";
$username = "xxx";
$password = "xxx";
$database_name = "xxx";
$table_name = "xxx";

mysql_connect($host, $username, $password) OR die("Can't
connect");
mysql_select_db($database_name) OR die("Can't connect to
Database");

$query = "SELECT Class FROM $table_name WHERE Initials = '".
$_SESSION['Initials']."' AND staff LIKE '%".$_SESSION['Surname']."'";
$result = mysql_query($query);
$class = mysql_fetch_array($result);
$count = mysql_num_rows($result);

if($count === NULL)
{
echo "ERROR";
}
else
{
$_SESSION['Class'] = $result;
echo "Class added to sessions";
}
}
?>


My initial problem where the query couldn't recognize the session variables was easily solved by adding the correct brackets for the if-statement. My next problem that has arisen here is that even though the query should be successfull (I don't receive an error message saying 'ERROR' when the $count is either FALSE or NULL) it's not creating the result array into a new session, because when I print the session array on a new page it's still only carrying over the 'Initials' and 'Surname' sessions.

What do I need to change to my query, or post-query process in order for that array (because it's bound to throw up multiple results) to be made into a new session?

Many thanks for the answers to my initial problem!

Answer

I've found the answer - it turned out that I wasn't treating one of the session variables as a proper array and thus wouldn't load properly. I've added my script below so that people with similar problems in the future can use it as a reference point:

<?php

 session_start();


 // Server Details //
 $host = "---";
 $username = "---";
 $password = "---";
 $database_name = "---";
 $table_name = "---";


 // Connect Command //
 mysql_connect($host, $username, $password) OR die("Can't  
 connect");
 mysql_select_db($database_name) OR die("Can't connect to 
 Database");


 // Query to call up the unique school name //
 $query_school = mysql_query("SELECT DISTINCT School FROM $table_name 
 WHERE Initials = '".$_SESSION['---']."'
 AND staff LIKE '%".$_SESSION['---']."'") or die( mysql_error());
 $result_school = mysql_result($query_school, 0);


 // Query to call up the unique centre no //
 $query_centreno = mysql_query("SELECT DISTINCT CentreNo FROM 
 $table_name WHERE Initials = '".$_SESSION['---']."'
 AND staff LIKE '%".$_SESSION['---']."'") or die( mysql_error());
 $result_centreno = mysql_result($query_centreno, 0);


 // The newly created sessions for school info //
 $_SESSION['---'] = $result_school;
 $_SESSION['---'] = $result_centreno;


 // Query to call up the array of classes //
 $query_class = mysql_query("SELECT Class FROM $table_name WHERE 
 Initials = '".$_SESSION['---']."'
 AND staff LIKE '%".$_SESSION['---']."'") or die( mysql_error());
 $query_class__array = array();
 while($row = mysql_fetch_assoc($query_class))
 $query_class_array[] = $row;

 $_SESSION['---'] = $query_class_array;

?>