简体   繁体   English

如何使用php在mysql中生成下一个自动增量数?

[英]How to generate next auto increment number in mysql using php?

I was trying to fetch next auto increment number in mysql using php. 我试图使用php在mysql中获取下一个自动增量数。 I tried this way: 我试过这种方式:

<?
$q=mysql_query("SELECT * FROM `users`");
$next_auto_inc=mysql_num_rows($q)+1;
?>

But, this when any row is deleted don't work. 但是,当删除任何行时,这不起作用。 I hope you got what I mean. 我希望你明白我的意思。 How can I do this using php? 我怎么能用PHP做到这一点?

You can't do that fetching the table data. 你不能这样做获取表数据。 You have to fetch the table status to get the auto increment number using php. 您必须使用php获取表状态以获取自动增量编号。 And that, you can do something like this: 那你可以这样做:

$q = mysql_query("SHOW TABLE STATUS LIKE 'test'");
$row = mysql_fetch_assoc($q);
$next_increment = $row['Auto_increment'];
echo "next increment number: [$next_increment]";

Hope this helps :) 希望这可以帮助 :)

[ Source ] [ 来源 ]

Assuming that you have user_id column as primary key you can also try this: 假设您有user_id列作为主键,您也可以尝试这样做:

$q = mysql_query('SELECT MAX(user_id) as user_id from `users`');
$row = mysql_fetch_assoc($q);
$next_auto_inc = $row['user_id'] + 1;

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

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