简体   繁体   English

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

[英]how to save in a session variable an array extracted from a database to send it to other web pages in 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']);当我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 ?那么,我该怎么做才能拥有所有数据?

The reason why your print_r looked good is that you put into the loop.您的 print_r 看起来不错的原因是您放入了循环中。 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.您每行重写 $_SESSION['med'] 变量,最后一行是您稍后打印会话时返回的结果。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM