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 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了