본문 바로가기
MySql

MySQL PHP / MYSQL : 데이터베이스의 모든 레코드를 반복합니다.

by 베이스 공부 2020. 10. 30.
반응형

나는 전체 php / mysql에 익숙하지 않습니다. 몇 주 분량의 서버 로그 (약 300,000 개 항목)가 있고 분석을 수행해야합니다. 나는 그것들을 모두 mysql db로 읽고 PHP로 분석 할 계획입니다.

내가 확신하지 못하는 것은 그것들을 반복하는 방법입니다. 파일을 읽는 자바를 사용하면 다음과 같이 할 수 있습니다.

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

PHP를 사용하여 mysql db의 모든 레코드를 어떻게 반복합니까? 나는 이와 같은 것이 어리석은 양의 메모리를 차지할 것이라고 생각합니다.

    $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
    }

그래서 저는 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;
}

 

해결 방법

 

여기를 보아라:



<?php
// Make a MySQL Connection
$query = "SELECT * FROM example"; 

$result = mysql_query($query) or die(mysql_error());


while($row = mysql_fetch_array($result)){
    echo $row['name']. " - ". $row['age'];
    echo "<br />";
}
?>


 

참조 페이지 https://stackoverflow.com/questions/4155257

 

 

반응형

댓글