marj marj - 9 months ago 37
PHP Question

Data stored in Session but when fetch in other php file some objects is null

On the original where I stored the data in session array, all data are complete when I fetch it but when I call that session on the other some data is null or undefined.

I don't know what I'm going to do, please help

Edit: In original file

$query = $dbh->prepare("Select p.start_date,p.end_date,dfp.id,dfp.product_id
dfp.type,dfp.quantity_required,dfp.less,pd.unit,pd.name
from promo p
inner join discounts_free_products dfp
on p.id=dfp.promo_id
inner join products pd
on dfp.product_id=pd.id
where DATE(p.end_date) >= DATE(NOW())
");
$query->execute();
$count=0;
$queryresult_set=array();
$queryresult_set=$query->fetchAll();
$count=count($queryresult_set);
$_SESSION['COUNT_PROMO']= $count;
$_SESSION["PROMO_PRODUCTS"]=$queryresult_set;


In other php file

foreach($_SESSION["PROMO_PRODUCTS"] as $result) {
$pc_list[] = $result["product_id"];
$d_id=$result['id'];
}


error:Undefined index: product_id

Answer Source

PHP 5 Session

A session is started with the session_start() function.

Session Create

<?php
    // Start the session
    session_start();
?>
<!DOCTYPE html>
<html>
<body>

<?php
    // Set session variables
    $_SESSION["favcolor"] = "green";
    $_SESSION["favanimal"] = "cat";
?>

</body>
</html>

To retrieve session on another page

<?php
    session_start();
?>
<!DOCTYPE html>
<html>
<body>

<?php
    // Echo session variables that were set on previous page
    echo "Favorite color is " . $_SESSION["favcolor"] . ".<br>";
    echo "Favorite animal is " . $_SESSION["favanimal"] . ".";
?>

</body>
</html>