Search code examples
phpmysqlauthentication

Get Current User ID from Mysql in php


I create a Users Database and users have username, password etc. And each one has a unique auto_increment id. I need this; For example if I login to the website it should be $id= my_id, if you login to the website it should be $id= your_id. Can you help please. Thanks.

I tried;

$result = mysql_query("select id from Users where username ='".$_SESSION['username']."'");

$row = mysql_fetch_array($result);

$id = $row['id']; 

Solution

  • In your login code (when you define $_SESSION['username']) you should store the user ID in the session, with any other user related data that you need.