尝试使用会话数组将多个 sql 变量从一个文件解析到另一个文件,但我不能。我在这里缺少的部分是什么?
提前致谢。
文件1.php
session_start();
while($row=mysql_fetch_array($result,MYSQL_BOTH)) {
echo "<tr>";
$id=$row['id'];
$array[] = $id;
}
$_SESSION['id'] = $array;
文件2.php
session_start();
foreach( $array as $id ) {
echo $id;
}
那是因为$array
不是您的共享变量。$_SESSION['id']
是你需要阅读的内容。
示例(取自:https : //web.archive.org/web/20080707052007/http : //www.phpriot.com/articles/intro-php-sessions/7):
页面1.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';
?>
页面2.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 />';
}
?>
正如评论中提到的echo
,page2.php 是 OTT。一个更简单的方法是:(echo "Value of {$_SESSION[$key]} is $value<br />" ;
信用:RiggsFolly)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句