PHP/MYSQL:迭代数据库中的每条记录

six*_*ude 3 php mysql loops

我是整个php/mysql的新手.我有一周的服务器日志(大约300,000项),我需要做一些分析.我打算将它们全部读入mysql数据库,然后用php分析它们.

我不确定的是如何迭代它们.使用java读取文件我会做这样的事情:

Scanner s = new Scanner(myfile);
while(s.hasNext()){
    String line = s.nextLine();
    ~~ Do something with this record. 
}
Run Code Online (Sandbox Code Playgroud)

如何使用php迭代mysql数据库中的所有记录?我认为像这样的东西需要愚蠢的记忆.

    $query = "SELECT * FROM mytable";
    $result = mysql_query($query);
    $rows = mysql_num_rows($result);
    for($j = 0; $j < $rows; ++$j){
            $curIndex   = mysql_result($result,$j,"index");
            $curURL     = mysql_result($result,$j,"something");
            ~~ Do something with this record
    }
Run Code Online (Sandbox Code Playgroud)

所以我在select语句中添加了一个限制,我重复一遍,直到所有记录都循环完毕.有更标准的方法吗?有没有内置的可以做到这一点?

while($startIndex < $numberOfRows){

    $query = "SELECT * FROM mytable ORDERBY mytable.index LIMIT $startIndex,$endIndex";
    $result = mysql_query($query);
    $rows = mysql_num_rows($result);
    for($j = 0; $j < $rows; ++$j){
            $curIndex   = mysql_result($result,$j,"index");
            $curURL     = mysql_result($result,$j,"something");
            ~~ Do something with this record
    }
    $startIndex = $endIndex + 1;
    $endIndex = $endIndes + 10;
}
Run Code Online (Sandbox Code Playgroud)

Ric*_*d H 5

SELECT * FROM MYTABLE如果你的桌子很大,你不想做一个,你将把整个事情放在记忆中.内存开销和数据库调用之间的权衡将是批处理请求.您可以在表中获取行的最小和最大ID:

SELECT MIN(ID) FROM MYTABLE;
SELECT MAX(ID) FROM MYTABLE;
Run Code Online (Sandbox Code Playgroud)

现在从minId循环到maxId,每次增加10,000.在伪代码中:

for (int i = minId; i < maxId; i = i + 10000) {
   int x = i;
   int y = i + 10000;
   SELECT * FROM MYTABLE WHERE ID >= x AND ID < y;
}
Run Code Online (Sandbox Code Playgroud)