获取下一个自动增量

发布于 2024-08-03 13:03:56 字数 244 浏览 6 评论 0原文

我知道这并不复杂,但我不记得该怎么做。

我只需要知道下一个自动增量。

$result = mysql_query("
    SHOW TABLE STATUS LIKE Media
");
$data = mysql_fetch_assoc($result);
$next_increment = $data['Auto_increment'];

...但我不会为我工作,我做错了什么?

I know this isn't so complicated but I can't remember how to do.

I just need to know the next auto increment.

$result = mysql_query("
    SHOW TABLE STATUS LIKE Media
");
$data = mysql_fetch_assoc($result);
$next_increment = $data['Auto_increment'];

...but i won't work for me, what am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

得不到的就毁灭 2024-08-10 13:03:56
$result = mysql_query("
    SHOW TABLE STATUS LIKE 'Media'
");
$data = mysql_fetch_assoc($result);
$next_increment = $data['Auto_increment'];

表的名称需要用单引号引起来,如下所示:'table_name'

所以现在工作得很好。

:)

$result = mysql_query("
    SHOW TABLE STATUS LIKE 'Media'
");
$data = mysql_fetch_assoc($result);
$next_increment = $data['Auto_increment'];

The name of the table needed to be wrapped with single quotes like this: 'table_name'

So it works just fine now.

:)

街角迷惘 2024-08-10 13:03:56

查询应如下所示:

SHOW TABLE STATUS WHERE `Name` = 'Media';

The query should look like this:

SHOW TABLE STATUS WHERE `Name` = 'Media';
我不在是我 2024-08-10 13:03:56

另一种方法,但速度较慢,是:

SELECT AUTO_INCREMENT FROM information_schema.`TABLES` T where TABLE_SCHEMA = 'myScheme' and TABLE_NAME = 'Media';

information_schema 对于从许多方案获取数据最有用。

Another way, but slow, is:

SELECT AUTO_INCREMENT FROM information_schema.`TABLES` T where TABLE_SCHEMA = 'myScheme' and TABLE_NAME = 'Media';

The information_schema is mostly usefull for getting data from many schemes.

停滞 2024-08-10 13:03:56

您也可以使用这个功能

function getNextValue(){
$query = "SHOW TABLE STATUS LIKE 'vendors'";
dbconnect();
$results=mysql_query($query);
if(mysql_errno() != 0) {
    $result['count'] = -1;
    $result['error'] = "Error: ".mysql_error();
} else {
    $result['count'] = mysql_num_rows($results);
    for($counter=0;$counter<$result['count'];$counter++) {
        $result[$counter] = mysql_fetch_assoc($results);
    }
}
return $result[0]['Auto_increment'];
mysql_close();
}

You can also use this function

function getNextValue(){
$query = "SHOW TABLE STATUS LIKE 'vendors'";
dbconnect();
$results=mysql_query($query);
if(mysql_errno() != 0) {
    $result['count'] = -1;
    $result['error'] = "Error: ".mysql_error();
} else {
    $result['count'] = mysql_num_rows($results);
    for($counter=0;$counter<$result['count'];$counter++) {
        $result[$counter] = mysql_fetch_assoc($results);
    }
}
return $result[0]['Auto_increment'];
mysql_close();
}
豆芽 2024-08-10 13:03:56
SELECT AUTO_INCREMENT
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = "database_name"
AND TABLE_NAME = "table_name";
SELECT AUTO_INCREMENT
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = "database_name"
AND TABLE_NAME = "table_name";
农村范ル 2024-08-10 13:03:56

如果您需要知道下一个 auto_increment,那么 99% 的可能性是您做错了。您应该只执行即将执行的插入操作,而不是获取下一个 auto_increment,然后使用 SELECT LAST_INSERT_ID() 从该插入中获取 auto_increment 值。

如果您尝试猜测下一个 auto_increment 值并且有多个用户同时执行此操作,您经常会得到错误的值。

if you need to know the next auto_increment, then it's 99% likely you're doing it wrong. instead of the getting the next auto_increment, you should just do the insert you're about to do, then use SELECT LAST_INSERT_ID() to get the auto_increment value from that insert.

if you try to guess the next auto_increment value and you have multiple users doing it at the same time, you'll frequently get the wrong value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文