Hello, there is a working user authorization on php for swift application. It is necessary to get and pass the user id, tell me how to add this code

header('Content-type: application/json'); if($_POST) { $username = $_POST['username']; $password = $_POST['password']; if($username && $password) { $db_name = 'mysite'; $db_user = 'root'; $db_password = '12345'; $server_url = 'localhost'; $mysqli = new mysqli('localhost', $db_user, $db_password, $db_name); /* check connection */ if (mysqli_connect_errno()) { error_log("Connect failed: " . mysqli_connect_error()); echo '{"success":0,"error_message":"' . mysqli_connect_error() . '"}'; } else { if ($stmt = $mysqli->prepare("SELECT login FROM users WHERE login = ? and password = ?")) { //$password = $password; // $id = "SELECT id FROM users WHERE login=?"; /* bind parameters for markers */ $stmt->bind_param("ss", $username, $password); /* execute query */ $stmt->execute(); /* bind result variables */ $stmt->bind_result($id1); /* fetch value */ $stmt->fetch(); /* close statement */ $stmt->close(); } /* close connection */ $mysqli->close(); if ($id1) { error_log("User $username: password match."); echo '{"success":1}'; } else { error_log("User $username: password doesn't match."); echo '{"success":0,"error_message":"Invalid Username/Password"}'; } } } else { echo '{"success":0,"error_message":"Invalid Username/Password."}'; } }else { echo '{"succ11ess":0,"error_message":"Invalid Data."}'; } ?> 

Thank you in advance!

  • And where is the table structure? - Naumov
  • And for the future in php there is a function json_encode() - Naumov
  • Well, as a structure, the table users in it id, login and password - J.Simon
  • It is to me in this code that I need to pass the id in order to use it later in the application. Can I stuff it in bind_param somehow, or how can I pass it on to something else? - J.Simon

1 answer 1

If your user id is stored in the id column, change the query slightly:

 SELECT id FROM users WHERE login = ? and password = ? 

Then in your $ id1 variable you will have a user id.

  • Thank you very much! Understood - J.Simon