php 如何在php的另一个页面中打印会话数组变量值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15014488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:23:03  来源:igfitidea点击:

how to print the session array variable values in another page in php

php

提问by Saravanan

this is the program to print the all subject name which are the field name of the student table.i stored the all subject name into an array and i have to print that array values in that another page.so i stored that array into session variable but while to print that array in next page it print only as "Array".

这是打印所有科目名称的程序,这些名称是学生表的字段名称。我将所有科目名称存储到一个数组中,我必须在另一页中打印该数组值。所以我将该数组存储到会话变量中但是在下一页打印该数组时,它仅打印为“数组”。

<?php

session_start();
$con=mysql_connect("localhost","root","");  
mysql_select_db("cse",$con);

$sql="select * from studenttable where username='saravanan'";

$calname=array();
$i=0;
mysql_query($sql);
while($rs=mysql_fetch_field($result))
{
         $colname[$i]=$rs->name;
         $i++;


}
$_SESSION['subject']=$calname;
?>


in the next page i print the session variable using 


<?php
session_start();
echo $_SESSION['subject'];
?>

but i am getting "Array"only as output.
how to print the all value of array what i stored into it already?

回答by Daedalus

Of course you're only getting Arrayas output.. you're attempting to convert an array to a string without the applicable function for doing so.

当然,您只是Array作为输出获得.. 您正在尝试将数组转换为字符串,而没有适用的函数来执行此操作。

If you want to print the full array, as a string, try print_r($array);.

如果要将完整数组打印为字符串,请尝试print_r($array);.

Further, you can of course join the array together using a string, using implode().. and last but not least, be sure to access the individual pieces of the array, should you want them, with their keys:

此外,您当然可以使用字符串将数组连接在一起,使用implode().. 最后但并非最不重要的是,如果需要,请确保使用它们的键访问数组的各个部分:

echo $_SESSION['subject']['somekeyhere'];

However, it would also appear that from your code, you actually have a few typos that would prevent you from getting the data you need: $calnameversus the $colnamevariable you have in your while loop.. leaves it as an empty array in your session.

但是,从您的代码看来,您实际上有一些拼写错误会阻止您获取所需的数据:$calname$colname您在 while 循环中拥有的变量相比......在会话中将其保留为空数组。

UPDATE 1:

更新1:

Another way to print the entire array.. or rather, echo out each individual value:

另一种打印整个数组的方法......或者更确切地说,回显每个单独的值:

foreach ($array as $key => $value) {
    echo $value . "<br />";
}

Off topic:

题外话:

I would not recommend using mysql_*functions to write new code. They are no longer maintained and the community has begun deprecation process. See the red box? Instead you should learn about prepared statementsand use either PDOor MySQLi. If you can't decide which, this articlewill help you. If you pick PDO, here is good tutorial. Also see Why shouldn't I use mysql functions in PHP?

我不建议使用mysql_*函数来编写新代码。它们不再被维护,社区已经开始弃用过程。看到红框了吗?相反,您应该了解准备好的语句并使用PDOMySQLi。如果你不能决定哪个,这篇文章会帮助你。如果您选择 PDO,这里有很好的教程。另请参阅为什么我不应该在 PHP 中使用 mysql 函数?

Example of your above code using PDO instead(with typos fixed as well):

上面使用 PDO 的代码示例(也修复了拼写错误):

try {
    $dbh = new PDO("mysql:host=localhost;dbname=cse", "root", "");
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e) {
    echo "Error!  Could not connect to database: " . $e->getMessage() . "<br/>";
    die();
}

$stmt = $dbh->prepare("select * from studenttable where username=:username");
$stmt->bindValue(":username","saravanan");
$stmt->execute();

$calname = array();
$i = 0;
while ($rs = $stmt->fetch()) {
     $calname[$i]=$rs['name'];
     $i++;
}
$_SESSION['subject']=$calname;

回答by Jon

Try adding with <pre>tag to get $_SESSION in more clearer view.

尝试添加<pre>标签以获得更清晰的 $_SESSION 视图。

    echo '<pre>';
    print_r($_SESSION);
    echo '</pre>';