0

所以这就是我正在做的事情。

  1. 在 for 循环中读取一行。(因为我是在共享主机中,所以一下子会占用一些资源。) 2.将正确的字段数据获取到变量中。3.根据提取的字段操作req数据。4.更新字段=提取数据的新字段。

另外一点,我正在将当前位置添加到文件中,以便脚本下次运行时可以从那里继续。

问题:它似乎不起作用。counter.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");
}

任何帮助将不胜感激:) 寻找代码优化并消除错误。建议会做。:)

4

2 回答 2

0

好吧,我会做这样的事情(抱歉延迟回复,我一直忘记)

<?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);
?>
于 2012-04-29T17:12:15.540 回答
-1

我在这里看到的 1 个问题:

检查您的更新查询 - 这似乎是错误的。据我说,它应该是“SET md2='$d1' AND md4='$d2'”

另一个我不确定的问题:

我不确定 md2 和 md4 是否是哈希算法的有效名称

这样做的更好方法: 1. 不要写入文件!2. 在 SQL 中创建一个名为“status”的附加列,默认值为 0。更新时,将该值更改为 1。3. 根据查询“SELECT word FROM collection WHERE status=0 limit”搜索要编辑的行0,1" 4. 或者如果原始表中的 md2 和 md4 列为空,查询也可以是 "SELECT word FROM collection WHERE md2='' and md4='' limit 0,1"

希望这可以帮助。

于 2012-04-18T09:29:36.880 回答