无法从数据库中提取id

时间:2014-05-10 22:59:45

标签: php mysql

我正在尝试从数据库中提取用户ID,但它没有出现。这是登录页面:

 <?php
    include ("dbconnect.php");      
    session_start();
    $username = $_POST["username"]; 
    $password=$_POST["password"];

$query=mysql_query("SELECT * FROM autori WHERE autori.username='".$username."' AND autori.password='".$password."'
UNION
SELECT * FROM editori WHERE editori.username='".$username."' AND editori.password='".$password."'
UNION
SELECT * FROM recenzori WHERE recenzori.username='".$username."' AND recenzori.password='".$password."';");

if(mysql_num_rows($query) == 1) 
{
        $rand = mysql_fetch_assoc($query);
        $_SESSION['user_id']    = $rand['id'];
        $_SESSION['username']   = $username;
        $_SESSION['password']   = $password;
        $_SESSION['user_type']  = $rand['id_usr'];

    if(isset($_SESSION['user_type']) && $_SESSION['user_type'] == 1) {
        echo "<script>alert('Sunteţi logat.');window.location.href='articole_pers.php';</script>";
    } else if(isset($_SESSION['user_type']) && $_SESSION['user_type'] == 3){
        echo "<script>alert('Sunteţi logat.');window.location.href='art_noi.php';</script>";
    } else if(isset($_SESSION['user_type']) && $_SESSION['user_type'] == 2){
        echo "<script>alert('Sunteţi logat.');window.location.href='articole_de_publicat.php';</script>";
    }
else { echo "<script>alert('Username or Email nu există !');window.location.href='index.php';</script>";
}
}
?>

在第二页上,我正在尝试获取数据,我使用:

echo $_SESSION['username']; //it works
echo $_SESSION['user_id']; //it doesn't work

如何从数据库中提取用户ID?

1 个答案:

答案 0 :(得分:0)

好的,首先,尝试使用PDO对象或SQLi,mysql_query已被弃用多年了。

确保$ rand [&#39; id&#39;]通过

获取值
print_r($rand)