nigel
nigel

Reputation: 11

echo integer from table with mysql

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?

Upvotes: 1

Views: 1647

Answers (1)

amosrivera
amosrivera

Reputation: 26554

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

Upvotes: 3

Related Questions