【问题标题】:PHP/MYSQL - How do i assign data fetched from the database(stored in variables) to a session variable?PHP/MYSQL - 如何将从数据库中获取的数据(存储在变量中)分配给会话变量?
【发布时间】:2016-03-16 23:21:26
【问题描述】:

我在传递从数据库中获取并存储在变量中的数组变量并将它们传递到会话变量以在整个站点上使用时遇到了一点问题。

代码:

$rid=$_SESSION['SESS_MEMBER_ID'] ;

$recquery = 'SELECT * FROM clinic_receptionist,clinic_table where clinic_receptionist.recep_clinic_id = clinic_table.ID AND recep_id ='.$rid;

$recresult = mysqli_query($conn,$recquery);

if(mysqli_num_rows($recresult)>0)
{
while($row = mysqli_fetch_assoc($recresult))
{
$cid = $row['clinic_id'];
$cname = $row['clinic_name'];
$name = $row['recep_full_name'];
$phone = $row['recep_mobile'];
$email = $row['recep_email'];
$address = $row['recep_address'];
$gender = explode(",",$row['recep_gender']);
$dob = $row['recep_dob'];
$doj = $row['recep_doj'];
}
}
?>

上面的代码是选择查询发生的地方。我想知道如何将$cid$cname 分配给不同页面中的$_SESSION 变量。任何帮助将非常感激。谢谢

代码:

<?php session_start();
include('header.php');

include('menu.php');
include('config.php');

$appquery = 'SELECT * FROM appointment_table,clinic_table where clinic_table.ID = appointment_table.clinic_id';
$appresult = mysqli_query($conn,$appquery);
if(mysqli_num_rows($appresult)>0)
{
while($row = mysqli_fetch_array($appresult))
{
$_SESSION['cid'] = $row['ID'];
$_SESSION['cname'] = $row['clinic_name'];
$ptdate = $row['date'];
$pttime = $row['time'];
$ptname = $row['pt_name'];
$ptphone = $row['pt_ph_num'];
$ptemail = $row['pt_email_id'];

}
}
?>

上面的代码是我想要接收 id 和诊所名称的地方。

【问题讨论】:

  • $_SESSION['id'] = $cid$_SESSION['name'] = $cname
  • 是否有任何特定的理由来打开和关闭这么多 php 标签而不是一个?那不是更整洁吗?
  • @bIgBoY 很抱歉。

标签: php mysql session


【解决方案1】:
<?php
    // Start the session
    session_start();

    //Assigning the values to $_SESSION
    $_SESSION['cid'] = $row['clinic_id'];

    $_SESSION['cname'] = $row['clinic_name'];;
    ?>

session_start

【讨论】:

  • 抱歉会话通常让我感到困惑,我如何获得与 $rid 匹配的相同 $cid
  • 你是指$rid对应的$cid吗?由于您在选择查询中给出了条件,因此只会产生对应的$cid
  • no $cid 对应于诊所 ID,$rid 对应于在特定诊所工作的接待员 ID。
  • 我认为您应该在接待员表中提及$cid 并加入诊所表。即每个接待员都应该有相应的诊所ID,对吗?如果是这样,我想应该这样做。
【解决方案2】:

你需要在每个页面上调用session_start()来初始化会话,当然;之后你就可以了

$_SESSION['cid'] = $cid;
$_SESSION['cname'] = $cname;

将这些值存储在$_SESSION

顺便说一句,有人会告诉你对那个查询使用准备好的语句,所以它可能是我。

也许是为了说明。

page1.php:

<?php
    session_start();  // find session, or create new one

    include('header.php');
    include('menu.php');
    include('config.php');

    $appquery = 'SELECT * FROM appointment_table,clinic_table where clinic_table.ID = appointment_table.clinic_id';

    $appresult = $conn->query($appquery);
    if(($rowcount = $appresult->num_rows()) > 0)
    {
        // create arrays to pass data
        $cids = array($rowcount);
        $cnames = array($rowcount);
        $i = 0;
        while($row = $appresult->fetch_array())
        {
            // add values to arrays
            $cids[$i] = $row['ID'];
            $cnames[$i] = $row['clinic_name'];
            $i++;
        }
        // add arrays to session
        $_SESSION['cids'] = $cids;
        $_SESSION['cnames'] = $cnames;
    }

page2.php:

<?php
    session_start();  // find session, or create new one

    include('header.php');
    include('menu.php');
    include('config.php');

    // retrieve arrays from session        
    $cnames = $_SESSION['cnames'];
    $cids = $_SESSION['cids'];

    // ... and print them
    $count = count($cids);
    for ($i = 0; $i < $count; $i++)
    {
        echo "cid = " . $cid[$i] . "; cname = " . $cname[$i] . "<br>";
    }

page1.php 执行查询并将cidcname 值存储在数组中,然后存储在$_SESSION 中。 page2.php$_SESSION 检索数组并将它们回显。当然,在生产中,在显示查询的页面上运行查询会被认为更有效,并且只在页面之间传递键值。

我还为mysqli 使用了对象语法而不是过程语法,只是为了展示它的样子。像往常一样,我将错误处理留给读者作为练习。

【讨论】:

  • 感谢您的回复,但它会知道会话是从哪里声明的吗?
  • 如果还没有打开会话,php 在浏览器中设置一个session_id cookie。对 session_start() 的下一次调用会查找该 cookie 以恢复会话。
  • session_start() 为您完成所有繁重的工作;您需要做的就是在每个页面的顶部调用它。
  • 我已将其添加到文件中,但是当我回显变量 $clinic_name = $_SESSION['cname']$clinic_id = $_SESSION['cid'] 时,它只会显示 Array
  • 嗨,达尔文,我已经编辑了代码,并添加了应该显示 cid 和 cname 的代码。如果可以,请看一下。
猜你喜欢
  • 2015-10-09
  • 2016-06-15
  • 2018-03-07
  • 2015-05-06
  • 2014-04-26
  • 2019-09-21
  • 1970-01-01
  • 2017-04-25
  • 2021-12-20
相关资源
最近更新 更多