Chris
Chris

Reputation: 1400

Read DB Write to flat file, times out between 15,000 & 35000 records

I am reading a MySQL database with a query that returns 187,000 records and I am writing the data to a flat file. It just stops without any error around 15,000 records to 35,000 records.

I thought maybe the database connection was timing out so I started pulling 10,000 records at a time with LIMIT, but it still happens. So I imagine it is either the browser or PHP that is timing out. Here is my code. If there is a better way of doing this I am totally open to hearing.

  $sql->Query($stype.$search);
$checkrows = $sql->rows;

if ($checkrows > 0){
   $fh = fopen($listname, 'w');
   for ($i = 0; $i < $sql->rows; $i++) {
     $sql->Fetch($i);
     $email .= $sql->data[1]."\n";
     fwrite($fh, $email);
     $cot++;
          echo $cot."-".$sql->data[1]."<br>";
   }
   fclose($fh);

 }

Upvotes: 1

Views: 309

Answers (4)

Chris
Chris

Reputation: 1400

My mistake was this line of code: echo $email .= $sql->data[1]."\n";

It should have been: echo $email = $sql->data[1]."\n";

Upvotes: 0

netcoder
netcoder

Reputation: 67745

This is not in PHP per say, but you can easily export your database to a flat-file format using MySQL only. Using a query like this one:

SELECT email FROM database.table 
  INTO OUTFILE '/path/to/file/foo.txt' 
  LINES TERMINATED BY '\n';

However, this will write to the same server running MySQL. You can also do a similar thing by using the MySQL command-line client to write locally:

mysql -u user --password=mypass \
  -e "SELECT email FROM database.table" \
  -B --skip-column-names > foo.txt

Upvotes: 1

cmendoza
cmendoza

Reputation: 296

If it is php timing out, try setting set_time_limit(0).

Upvotes: 1

Mano Kovacs
Mano Kovacs

Reputation: 1514

Add set_time_limit(600); to the top! Your scripts times out.

Upvotes: 0

Related Questions