dongtanliefang8765 2013-10-10 10:29
浏览 60
已采纳

查询后没有创建新的$ _SESSION变量?

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!

  • 写回答

2条回答 默认 最新

  • doujingtang6580 2013-10-11 10:34
    关注

    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;
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog