PHP and MySql ID's


Basically I have setup up SESSIONS on my site.

Once a user registers an Auto Incremented ID is made for this user (I hope that is the right word)

What I want to do is once the POST method is sent and the user is signed up on the next page say if the action is Welcome.php I would like on the Welcome.php page to echo the users ID

Is this possible ?

If so how ?




You will want to call this function after the insert:




It sounds like you want a basic introduction to sessions. Everything you need should be in that link but as a very basic guide:

On both pages addsession_start(); before you output any code.

You can then store and read variables in the$_SESSION superglobal:

// Set this when the user registers
$_SESSION['userId'] = mysql_insert_id(); 

// This will work on any page now, after the user logs in
echo $_SESSION['userId']; 



First of all I don't know why you increment user ID after every sign up. Seems awkward for me. I assume this value is stored in the database. Just run a SQL query that will fetch this value for you.

If you are using sessions then you have to have some identifier stored in$_SESSION to actually tell which user is that (like email, ID, whatever). Use this value to query the db. It's hard to give the exact solution without knowing the table schema but something like this should do the trick (I'm assuming that you store user email in the session to identify it)

$q = "SELECT id FROM `users` WHERE `email` = '{$_SESSION['email']}'";
$r = mysql_query($q);
$res = mysql_fetch_num($r);
$userId = $res[0];



I think mysql_insert_id() is what you need. Basically, you should call this function right after you insert to get the latest added id.

People are also looking for solutions to the problem: php - unwanted character


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.