简体   繁体   English

用mysql从表中回显整数

[英]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. 表格列之一是“ id”,其中为用户提供了参考编号。 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. 为了添加新用户,我必须选择最后注册的用户,提取ID,向其添加1,然后将其提交回给新用户的新行中。 I'm currently using: 我目前正在使用:

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

to select the maximum ID number. 选择最大ID号。 When I added 1 to that and tried to echo, it printed "Request ID#3" in the browser. 当我在其中添加1并尝试回显时,它在浏览器中打印了“请求ID#3”。

How do I write a script that will take the highest ID, add one, and use it for the new registering user? 如何编写一个具有最高ID的脚本,添加一个ID,并将其用于新的注册用户?

You need to use mysql_fetch_array() first before echoing the result like: 在回显结果之前,需要先使用mysql_fetch_array()

$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. 但是这不是必须的,您可以将字段设置为auto increment并且每次在数据库中插入插入时它将自动添加1。 Read more about it here: 在此处了解更多信息:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM