how to print the session array variable values in another page in php
php
Solution
Of course you're only getting `Array` as output.. you're attempting to convert an array to a string without the applicable function for doing so.
If you want to print the full array, as a string, try `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:
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: `$calname` versus the `$colname` variable you have in your while loop.. leaves it as an empty array in your session.
UPDATE 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 statements and use either PDO or MySQLi. If you can't decide which, this article will help you. If you pick PDO, here is good tutorial. Also see Why shouldn't I use mysql functions in PHP?
Example of your above code using PDO instead(with typos fixed as well):
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;
Problem
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? ```