简体   繁体   中英

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:

<?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_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 . All you need to do is add a session_start(); at the top of any php script where you need to use session.

<?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.

echo $_SESSION["user_id"] ;

Let's start from the beginning. You create a login form, you store sessions based on the values:

login.php

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

main.page

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

EDIT 2

Ok, so you want to check if username exists and then echo their ID. Regardless, almost all login systems have sessions.

After logging in, let's say you have a $_SESSION of id .

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 . However, the OP requested to echo the id from the db, so I echoed $id2 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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