dov11020 2019-03-13 11:37
浏览 52
已采纳

如何在会话变量中保存从数据库中提取的数组,以将其发送到php中的其他网页

I have some data than I recoved from a database in a treatment page and than i try to send these data to other web pages by a session variable, like that :

while($enr = mysqli_fetch_assoc($res))
{
    $_SESSION['med'] = $enr;

    header("location: recherche.php");

    //print_r($_SESSION['med']);
}

when I print_r($_SESSION['med']); in the processing page, I have an array like that :

Array ( [nom] => CASPER [prenom] => ARMAND ) 
Array ( [nom] => WILLIAMS [prenom] => GEORGE ) 
Array ( [nom] => VANASTEN [prenom] => ROBERT ) 
Array ( [nom] => MARTIN [prenom] => ALAIN ) 
Array ( [nom] => Jacque [prenom] => ERIC ) 
Array ( [nom] => LUCAS [prenom] => ANNIE )

But when I try to retrieve this array of data to other pages like that :

<?php
        if (isset($_SESSION['med'])) {
            foreach ($_SESSION['med'] as $champ) {
                echo "$champ -----";
            }
        } else {
            echo "no data";
        }
?>

I only have the last like that :

LUCAS -----ANNIE -----

So, how can I do to have all the data ?

  • 写回答

1条回答 默认 最新

  • dongzouya5792 2019-03-13 11:44
    关注

    The reason why your print_r looked good is that you put into the loop. You rewrite the $_SESSION['med'] variable every row and the last row was your result what you get back when you print out your session later.

    You should try this:

      while($enr = mysqli_fetch_assoc($res))
        {
            $_SESSION['med'][] = $enr;
    
    
    
    
        }
    //print_r($_SESSION['med']);
    header("location: recherche.php");
    

    And then:

    if (isset($_SESSION['med'])) {
            foreach ($_SESSION['med'] as $champ) {
                echo $champ['nom']." -----";
             }
     } else {
          echo "no data";
     }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看