简体   繁体   English

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

[英]get ID for a specific user in mysql database in PHP

When a user signs in i want to echo back there ID (which is created because of the auto_increment in phpMyAdmin) from there account, here's my login.PHP: 当用户登录时,我想从那里帐户回显那里的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);


?>

Add the column name id in your sql query.let say your column name for id is ID 在您的SQL查询中添加列名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";

}

Since your making login in php its good choice to use $_SESSION . 由于您使用php登录,因此使用$_SESSION一个不错的选择。 All you need to do is add a session_start(); 您需要做的就是添加一个session_start(); at the top of any php script where you need to use session. 在您需要使用会话的任何PHP脚本的顶部。

<?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";

    }

Now you can access anywhere in your php script using the $_SESSION variable. 现在,您可以使用$_SESSION变量访问PHP脚本中的任何位置。

echo $_SESSION["user_id"] ;

Let's start from the beginning. 让我们从头开始。 You create a login form, you store sessions based on the values: 您创建一个登录表单,并根据以下值存储会话:

login.php 的login.php

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

main.page 主页

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

EDIT 2 编辑2

Ok, so you want to check if username exists and then echo their ID. 好的,因此您想检查用户名是否存在,然后回显其ID。 Regardless, almost all login systems have sessions. 无论如何,几乎所有登录系统都有会话。

After logging in, let's say you have a $_SESSION of id . 登录后,假设您具有id$_SESSION

php PHP

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."
} 

Please note, normally, I would just echo $id . 请注意,通常,我只会回显$id However, the OP requested to echo the id from the db, so I echoed $id2 . 但是,OP请求从数据库回显id,因此我回显了$id2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM