这就是我正在做的事情。
- 在 for 循环中各读一行。(因为我在共享主机中,一下子会占用一些资源。2.为变量获取正确的字段数据。3.根据提取的字段操作请求数据。4.更新归档=提取数据的新字段。
补充一点,我正在将当前位置添加到文件中,以便脚本下次运行时可以从那里继续。
问题 :它似乎不起作用。计数器.txt获取类似 3-4 的值,但它只是驻留在那里。我的数据库有大约 1000k 行。
我的代码 :
require ("dbconnect.php");
header("refresh:29;url=process.php"); // so it doesnt ever end. I cant use max_execution_time here for some reason.
$count = mysql_query("SELECT COUNT(*) FROM collection ");
$data = mysql_fetch_array($count);
$count = $data[0];
echo $count;
$countfile = fopen("counter.txt", "r");
$counter = fgets($countfile);
echo fgets($countfile);
while (fgets($countfile) <= $count)
{
$i = fgets($countfile);
$takeword = mysql_query("SELECT word FROM collection WHERE id='$i'") or die();
$wd = mysql_fetch_array($takeword);
$data = $wd[0];
$d1 = hash($algorith='md2',$data);
$d2 = hash($algorith='md4',$data);
$write = mysql_query("UPDATE collection SET md2='$d1', md4='$d2' WHERE id='$i'") or die(mysql_error());
//opens, empties and write the new pointer to the file. closes, and open the file in readmode for the next read at the loop.
$counts = fopen("counter.txt", "w+");
fwrite($counts, $counter + 1);
fclose($counts);
$countfile = fopen("counter.txt", "r");
}
任何帮助将不胜感激:)寻找代码优化并杀死错误。建议就可以了:)
好吧,我会做这样的事情(对不起延迟的响应,我一直忘记了)
<?php
//main execution
$sql = mysql_connect(...);
if (!$sql)
die ("No database connection");
if (!mysql_select_db(..., $sql))
die ("Database does not exist in this schema");
//Run the query for this iteration.
processQuery();
//---
function getQueryOffset($file)
{
$offset = 0; //default offset
if (file_exists($file)) //check if the counter file exists
{
$contents = file_get_contents($file); //get the contents of the counter
if ($contents !== FALSE && is_numeric($contents)) //check if an appropriate counter value
$offset = intval($contents);
}
return $offset;
}
function processQuery()
{
$table = "collection"; //table to update
$counter = "counter.txt"; //where to look for the last execution's offset.
$maxrows = 10000; //update 10,000 rows each time this file is loaded.
$sql = $GLOBALS['sql'];
//calculate the number of rows in the table
$qCount = mysql_query("SELECT COUNT(*) max FROM $table", $sql);
$aCount = mysql_fetch_assoc($qCount);
mysql_free_result($qCount);
$max = $aCount["max"];
$offset = getQueryOffset($counter); //calculate the offset (or a default 0)
if ($offset < $max) //if offet >= max, we're done.
{
$qUpdate = mysql_query("SELECT word, id FROM $table LIMIT $offset, $maxrows", $sql); //get the next "maxrows" rows from the table.
if ($qUpdate)
{
$assoc = NULL;
while (($assoc = mysql_fetch_assoc($qUpdate)) != NULL)
{
$md4 = hash("md4", $assoc["word"]); //calculate the hashes
$md2 = hash("md2", $assoc["word"]);
$id = $assoc["id"]; //id the row
mysql_query("UPDATE $table SET md2='$md2', md4='$md4' WHERE id=$id", $sql); //update the table columns
}
//update the offset in the counter file.
file_put_contents($counter, ($offset + mysql_num_rows($qUpdate)));
mysql_free_result($qUpdate);
}
}
}
mysql_close($sql);
?>
我在这里看到的 1 个问题:
检查您的更新查询 - 这似乎是错误的。根据我的说法,它应该是"SET md2='$d 1' 和 md4='$d 2'"
我不确定的另一个问题:
我不确定 md2 和 md4 是否是哈希算法的有效名称
更好的方法:1.不要写入文件!2. 在 SQL 中创建一个名为"status"的附加列,默认值为 0。更新时,将该值更改为 1。3.根据查询"从集合中选择单词"搜索要编辑的行,其中状态= 0限制0,1"4. 或者,如果原始表中的 md2 和 md4 列为空,则查询也可以是"从集合中选择单词,其中 md2='' 和 md4='' 限制 0,1"
希望这有帮助。