gpt4 book ai didi

php mysql游标

转载 作者:行者123 更新时间:2023-11-29 01:04:57 27 4
gpt4 key购买 nike

我需要创建一个 MySQL 游标来跟踪我当前在遍历“巨大”(数百万个)表时的行号。

示例数据库表:

CREATE TABLE  test (
id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
someText TEenter code hereXT NOT NULL
) ;

如果此表为 1,000,000 个条目;我执行以下查询:

select * from test where id >= 50;

然后我根据需要在我的 php 脚本中处理数据(限制为 1 分钟)。我如何跟踪我遍历“测试”表的行数?

最佳答案

// use a PHP session to store the id (could also use cookies...)
session_start();

// your 1 minute timeout
set_time_limit(60);

// query your results (may even put a known-out-of-reach limit on the
// query just to make sure you're not always pulling all the entries every
// reload (that would each up your timeout alone, depending)
$lastID = 0; // lowest possible ID value (e.g. MIN(id) )
if (session_is_registered('last_select_id'))
{
$lastID =(int)$_SESSION['last_select_id'];
}
else
{
session_register('last_select_id');
$_SESSION['last_select_id'] = $lastID;
}
$dbResult = mysql_query("SELECT * FROM test WHERE id>{$lastID} ORDER BY id"/* LIMIT 10000 */);
if ($dbResult)
{
while (($row = mysql_fetch_row($dbResult)) !== false)
{
// perform processing

// mark as last processed (depending how many steps,
// you may decide to move this from end to somewhere
// else, just sh*t luck where your timeout occurs)
$_SESSION['last_select_id'] = $row['id'];
}
}

// it reached the end, save to assume we can remove the session variable
session_unregister('last_select_id');

我只能按照你告诉我的去做,虽然我觉得这应该被本地限制,而不仅仅是等待 PHP 吐出超时。

编辑另一种扩展批处理想法的方法是更改​​表并添加一个您可以更新的“已处理”列。

EDIT2 另外,要小心。我确实设置/取消设置 session 值。出于这个原因,即使您成功返回页面而不是超时错误($lastID 不会看到 session 变量,它会从 1 重新开始,然后继续再次)。

关于php mysql游标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4664124/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com