1

I'm building a user database for my website. One of the table columns is "id" where the user is given a reference number. In order to add new users, I have to select the last-registered user, extract the ID, add 1 to it, then submit that back into a new row for the new user. I'm currently using:

$query = mysql_query("SELECT MAX(id) FROM users;");

to select the maximum ID number. When I added 1 to that and tried to echo, it printed "Request ID#3" in the browser.

How do I write a script that will take the highest ID, add one, and use it for the new registering user?

1
  • Why don't you use AUTO_INCREMENT on that column instead? Commented Jan 15, 2011 at 5:02

1 Answer 1

3

You need to use mysql_fetch_array() first before echoing the result like:

$query = mysql_query("SELECT MAX(id) FROM users;");
$result = mysql_fetch_array($query);

echo $result["id"];

BUT this is not necessary you can set the field to auto increment and it will add 1 automatically everytime your insert somtehing in the DB. Read more about it here:

http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.