PHP / MYSQL - 如何将从数据库中获取的数据(存储在变量中)分配给会话变量?

时间:2015-12-11 05:44:15

标签: php mysql session

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

代码:

$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变量。任何帮助将非常感激。感谢

CODE:

<?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'];

}
}
?>

以上代码是我希望收到身份证和诊所名称的地方。

2 个答案:

答案 0 :(得分:3)

    final String username = "yoruusername@gmail.com";
    final String password = "yorupasswors";

    //set the following configs as follows

    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.port", "587");

session_start

答案 1 :(得分: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的对象语法而不是程序语法,只是为了展示它的外观。像往常一样,我将错误处理留给读者练习。