在Session变量中存储变量并显示到下一页

时间:2016-01-25 17:06:03

标签: php session

我试图将我从查询中获取的变量存储到$ _SESSION变量。

$sql = "SELECT id_user FROM signup ORDER BY id_user DESC LIMIT 1";

$sql_query=mysqli_query($connection,$sql);
if(!$sql_query){
die('Could not query:' . mysql_error());
}
print_r ($sql_query);

session_start();
$_SESSION["id_user"] = $sql_query;

现在我想把它显示到下一页,我将使用它。

session_start();
echo $_SESSION["id_user"];

但是,当我试图回应时,它表示不能将其转换为字符串,否则它将回显#34; 1"而不是我添加到表中的最后一个元素。 提前致谢

1 个答案:

答案 0 :(得分:1)

这里有很多错误

第1页

// always start the session at top of script
session_start();

$sql = "SELECT id_user FROM signup ORDER BY id_user DESC LIMIT 1";

$sql_query = mysqli_query($connection,$sql);

if(!$sql_query){

    //die('Could not query:' . mysql_error());

    /*
       You cannot use mysqli_ to connect and then mysql_
       functions to process anything. 
       THEY ARE COMPLETELY DIFFERENT ANIMAL
    */
    die('Could not query:' . mysqli_error($connection));
}

/* 
   Now read the result set 
   so you have access to the row returned
*/
$row = mysqli_fetch_assoc($sql_query)

$_SESSION["id_user"] = $row['id_user'];

第2页现在应该看到Session

中的值
session_start();
echo $_SESSION["id_user"];