P.Konstantyn P.Konstantyn - 5 months ago 15
SQL Question

How to get the user_id created in Database using PHP?

I try to use SESSION or without. Maybe I have a issues using SESSION.
In phpMyAdmin I am using

userid int(11) auto_increment
.

All what created using POST method work.

<?php
session_start();
$con = mysqli_connect("mysql11.000webhost.com", "a7083778_user", "abcd12734", "a70830778_data");

$username = $_POST["username"];
$password = $_POST["password"];

$statement = mysqli_prepare($con, "SELECT * FROM user WHERE username = ? AND password = ?");
mysqli_stmt_bind_param($statement, "ss", $username, $password);
mysqli_stmt_execute($statement);

mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement, $userID, $name, $age ,$username, $password, $userPoints);

$response = array();
$response["success"] = false;
$_SESSION["userid"] = $userID;
while(mysqli_stmt_fetch($statement)){
$response["success"] = true;
$response["userID"] = $_SESSiON["userid"];
$response["name"] = $name;
$response["age"] = $age;
$response["username"] = $username;
$response["password"] = $password;
$response["userPoints"] = $userPoints;
}

echo json_encode($response);
?>

Answer

Please try this,i think it will work for you

<?php
session_start();    
$conn = new mysqli("mysql11.000webhost.com", "a7083098_user", "abcd1234", "a7083098_data");
$username = $_POST["username"];
$password = $_POST["password"];
$qry = "SELECT * FROM user WHERE username = $username AND password = $password";
$result =  $conn->query($qry);
$response = array();
while($row = $result->fetch_assoc())
{
	$response["success"] = true;
	$response["userID"] = $row['userId'];
	$_SESSiON["userid"] = $response["userID"];   //Store userId in session
	$response["name"] = $row['name'];
	$response["age"] = $row['age'];
	$response["username"] = $row['username'];
	$response["password"] = $row['password'];
	$response["userPoints"] = $row['userPoints'];
}
echo json_encode($response);
session_destroy();

?>

Comments