获取PHP中mysql数据库中特定用户的ID

时间:2016-03-11 02:52:21

标签: php mysql database mysqli

当用户登录时,我希望通过那里帐户回显ID(由于phpMyAdmin中的auto_increment创建),这里是我的login.PHP:

<?php

$conn = mysqli_connect("xxxxxx", "xxxxx", "xxxx", "BuyerAccounts");

$Email = $_POST["Email"];
$Password = $_POST["Password"];


$sql_query = "select Buyer_Email from user_info where Buyer_Email like '$Email' and Buyer_Password like '$Password';";

$result = mysqli_query($conn, $sql_query);

if(mysqli_num_rows($result) > 0 ){

$row = mysqli_fetch_assoc($result);
$name = $row["Buyer_Email"]; 
echo "Welcome: Buyer";

}else{
$int = 1;
//echo "Buyer login failed...";
}
}else{
echo "Login failed...";
}
}


mysqli_stmt_close($statement);

mysqli_close($conn);


?>

2 个答案:

答案 0 :(得分:3)

在sql query.let中添加列名称id,表示id的列名称为 ID

$sql_query = "select ID,Buyer_Email from user_info where Buyer_Email like '$Email' and Buyer_Password like '$Password';";
$result = mysqli_query($conn, $sql_query);

if(mysqli_num_rows($result) > 0 ){

$row = mysqli_fetch_assoc($result);
$name = $row["Buyer_Email"]; 
$user_id =  $row['ID'];
echo $user_id;
echo "Welcome: Buyer";

}

由于您在php中登录是使用$_SESSION的不错选择。 您需要做的就是在需要使用会话的任何php脚本的顶部添加session_start();

<?php
session_start();
$conn = mysqli_connect("xxxxxx", "xxxxx", "xxxx", "BuyerAccounts");

$Email = $_POST["Email"];
$Password = $_POST["Password"];
 $sql_query = "select ID,Buyer_Email from user_info where Buyer_Email like '$Email' and Buyer_Password like '$Password';";

    $result = mysqli_query($conn, $sql_query);

    if(mysqli_num_rows($result) > 0 ){

    $row = mysqli_fetch_assoc($result);
    $name = $row["Buyer_Email"]; 
    $user_id =  $row['ID'];

    //using session
    $_SESSION["user_id"] = $user_id;

    echo $user_id;
    echo "Welcome: Buyer";

    }

现在,您可以使用$_SESSION变量访问php脚本中的任何位置。

echo $_SESSION["user_id"] ;

答案 1 :(得分:0)

让我们从头开始。您创建登录表单,根据值存储会话:

<强>的login.php

session_start();
$_SESSION["username"] = $username;

<强> main.page

$username = $_SESSION["username"];
echo "Hi $username";

编辑2

好的,所以你要检查用户名是否存在,然后回显他们的ID。无论如何,几乎所有登录系统都有会话。

登录后,假设您有{{1>} ID

<强> PHP

$_SESSION

请注意,通常情况下,我会回复session_start(); $id = $_SESSION["id"]; $db = mysqli_connect("xxxxxx", "xxxxx", "xxxx", "BuyerAccounts"); $check = $db->query("SELECT * FROM users WHERE id='$id'"); $num_check = $check->num_rows; $fetch_check = $check->fetch_object(); $id2 = $fetch_check->id; if($num_check) { // User Exists echo $id2; } else { echo "You don't exist." } 。但是,OP要求回显db的id,所以我回复了$id