-2

I have a really hard time understanding sessions. I want to store card_idin sessions , how do i do that?

$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";
Fredrik
  • 191
  • 1
  • 9

3 Answers3

1

You can use $_SESSION throughout your application. To store sessions you'll always need to start with session_start();

If we look at your example:

<?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";
    }

Now we can use this session variable on another page for example test.php:

 <?php 
 session_start();
 echo $_SESSION['card_id']; //Echo's the id
Daan
  • 12,099
  • 6
  • 34
  • 51
0

Try this code use 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;
?>
Abhishek Sharma
  • 6,689
  • 1
  • 14
  • 20
0

Try using this:

<?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";
    }
?>
Navnish Bhardwaj
  • 1,687
  • 25
  • 39