duanbeng6709 2015-09-29 05:48
浏览 130
已采纳

数据存储在Session中但是当在其他php文件中获取某些对象时为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

  • 写回答

2条回答 默认 最新

  • douying2243 2015-09-29 05:52
    关注

    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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大