使用PHP中的会话数组解析SQL变量

时间:2022-09-20 12:22:38

trying to parse multiple sql variables from one file to another using session arrays but i cant. Whats the part iam missing here?

尝试使用会话数组解析从一个文件到另一个文件的多个sql变量,但我不能。什么是我在这里失踪的部分?

Thanks in advance.

提前致谢。

file1.php

session_start();
while($row=mysql_fetch_array($result,MYSQL_BOTH)) {
    echo "<tr>";
    $id=$row['id'];
    $array[] = $id;
}
$_SESSION['id'] = $array;

file2.php

session_start();
foreach( $array as $id ) {
    echo $id;
}

2 个解决方案

#1


1  

That's because $array isn't your shared variable. $_SESSION['id'] is what you need to be reading from.

那是因为$ array不是你的共享变量。 $ _SESSION ['id']是您需要阅读的内容。

Example (taken from: https://web.archive.org/web/20080707052007/http://www.phpriot.com/articles/intro-php-sessions/7):

示例(摘自:https://web.archive.org/web/20080707052007/http://www.phpriot.com/articles/intro-php-sessions/7):

Page1.php

<?php
// begin the session
session_start();

// create an array
$my_array=array('cat', 'dog', 'mouse', 'bird', 'crocodile', 'wombat', 'koala', 'kangaroo');

// put the array in a session variable
$_SESSION['animals']=$my_array;

// a little message to say we have done it
echo 'Putting array into a session variable';
?>

Page2.php

<?php
// begin the session
session_start();

// loop through the session array with foreach
foreach($_SESSION['animals'] as $key=>$value)
    {
    // and print out the values
    echo 'The value of $_SESSION['."'".$key."'".'] is '."'".$value."'".' <br />';
    }
?>

As mentioned in the comments, the echo on page2.php is OTT. A simpler method would be: echo "Value of {$_SESSION[$key]} is $value<br />" ; (credit: RiggsFolly)

正如评论中所提到的,page2.php上的echo是OTT。一个更简单的方法是:echo“{$ _SESSION [$ key]}的值是$ value
”; (信用:RiggsFolly)

#2


1  

In your code example you're not reading $array from $_SESSION.

在你的代码示例中,你不是从$ _SESSION中读取$ array。

Also keep in mind that $_SESSION stores everything as strings unless you implement your own session handler.

还要记住$ _SESSION将所有内容存储为字符串,除非您实现自己的会话处理程序。

#1


1  

That's because $array isn't your shared variable. $_SESSION['id'] is what you need to be reading from.

那是因为$ array不是你的共享变量。 $ _SESSION ['id']是您需要阅读的内容。

Example (taken from: https://web.archive.org/web/20080707052007/http://www.phpriot.com/articles/intro-php-sessions/7):

示例(摘自:https://web.archive.org/web/20080707052007/http://www.phpriot.com/articles/intro-php-sessions/7):

Page1.php

<?php
// begin the session
session_start();

// create an array
$my_array=array('cat', 'dog', 'mouse', 'bird', 'crocodile', 'wombat', 'koala', 'kangaroo');

// put the array in a session variable
$_SESSION['animals']=$my_array;

// a little message to say we have done it
echo 'Putting array into a session variable';
?>

Page2.php

<?php
// begin the session
session_start();

// loop through the session array with foreach
foreach($_SESSION['animals'] as $key=>$value)
    {
    // and print out the values
    echo 'The value of $_SESSION['."'".$key."'".'] is '."'".$value."'".' <br />';
    }
?>

As mentioned in the comments, the echo on page2.php is OTT. A simpler method would be: echo "Value of {$_SESSION[$key]} is $value<br />" ; (credit: RiggsFolly)

正如评论中所提到的,page2.php上的echo是OTT。一个更简单的方法是:echo“{$ _SESSION [$ key]}的值是$ value
”; (信用:RiggsFolly)

#2


1  

In your code example you're not reading $array from $_SESSION.

在你的代码示例中,你不是从$ _SESSION中读取$ array。

Also keep in mind that $_SESSION stores everything as strings unless you implement your own session handler.

还要记住$ _SESSION将所有内容存储为字符串,除非您实现自己的会话处理程序。