PHP 变量引用问题


PHP variable referencing issue

我正在尝试在用户登录时从数据库中读取用户的id,并将其保存到变量中以供以后使用。 我的用户表是这样的

    addressBookUsers
[
userid int(11) PK AUTO_INCREMENT;
firstName;
LastName;
email
]

有一些虚拟数据

userid username password
1      fred     12ewerefds2
2      al        343ed3fe

这是我使用用户名获取用户 ID 并存储到变量中的代码

    <?php
    session_start();
    include("dbconnect.php");
    $con= new dbconnect();
    $con->connect();
    //create and issue the query
    $id = "SELECT userid FROM addressBookUsers WHERE username = '".$_POST["username"]."'";
    $userid = mysql_query($id);
    while($row = mysql_fetch_array($userid)) {
    $me = $row[0]}
    $se=$me;

    echo($se)
?>

这将返回正确的用户 ID,但是当我尝试在另一个 PHP 文件中调用 $se 以查看它是否已保存时,我没有得到 resul

test.php
<?php
include ("userloginses.php");
echo $se;
?>

我不确定为什么 $se int 没有通过测试.php

有什么帮助吗?是的,有一些来自未包含内容的 html,但这与手头的问题无关

你做错了。您有会话,因此请使用它们:

$_SESSION['se'] = $me;

然后测试.php看起来像这样:

<?php
    session_start();
    include ("userloginses.php");
    echo $_SESSION['se'];
?>

你可以像这样引用任何PHP变量。如果你想使用PHP变量甚至任何Web语言的保留值,你必须将其保存到SESSION或COOKIE中。在用户登录的情况下,您应该使用 SESSION 变量。在你的代码启动会话中,而不是定义 $_SESSION['e'] 并访问它目录的任何 php 脚本,而不是$e。不要忘记在要访问此变量的每个 php 脚本的第一行中使用 session_start() 启动会话。