开发者

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 n开发者_高级运维ew 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?


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜