使用会话

时间:2015-10-19 11:40:47

标签: php mysql sql session mysqli

我很难理解会话。我想在会话中存储card_id,我该怎么做?

$resultSet = $mysqli->query ("SELECT value FROM card_credit 
WHERE card_id= (select distinct card_id 
                from Raw where 
                 id = (select max(b.id) from Raw as b))");

if($resultSet->num_rows != 0){

while($rows = $resultSet->fetch_assoc())
    {
        $card_id = $rows['value'];

        echo "<p>Saldo: $card_id";

3 个答案:

答案 0 :(得分:1)

您可以在整个申请过程中使用$_SESSION。 要存储会话,您始终需要从session_start();

开始

如果我们看一下你的例子:

<?php
session_start();
$resultSet = $mysqli->query ("SELECT value FROM card_credit 
                WHERE card_id= (select distinct card_id 
                from Raw where 
                 id = (select max(b.id) from Raw as b))");

if($resultSet->num_rows != 0){

while($rows = $resultSet->fetch_assoc())
    {
        $card_id = $rows['value'];
        $_SESSION['card_id'] = $card_id; //Set session variable
        echo "<p>Saldo: $card_id";
    }

现在我们可以在另一个页面上使用此会话变量,例如test.php:

 <?php 
 session_start();
 echo $_SESSION['card_id']; //Echo's the id

答案 1 :(得分:0)

请尝试使用此代码session_start

<?php
// Start the session
session_start();
$resultSet = $mysqli->query ("SELECT value FROM card_credit 
WHERE card_id= (select distinct card_id 
                from Raw where 
                 id = (select max(b.id) from Raw as b))");

if($resultSet->num_rows != 0){

while($rows = $resultSet->fetch_assoc())
    {
        $card_id = $rows['value'];

        echo "<p>Saldo: $card_id";
        $_SESSION['card_id']=$card_id;
?>

答案 2 :(得分:0)

尝试使用:

<?php
    // Start the session
    session_start();

    $resultSet = $mysqli->query ("SELECT value FROM card_credit WHERE card_id= (select distinct card_id from Raw where id = (select max(b.id) from Raw as b))");

    if($resultSet->num_rows != 0){
        $card_id = $rows['value'];
        // Set session variables
        $_SESSION["card_id"] = $card_id;
        //echo "Session variables are set.";
        echo "<p>Saldo: $card_id";
    }
?>