Forum Moderators: coopster

Message Too Old, No Replies

Intermediate Question: use Count Function()

Function() Count PHP

         

benghee

3:19 am on Jun 18, 2008 (gmt 0)

10+ Year Member



Hi all,

I wrote an update database code but since my csv file is too large for server handle, it exceeded maximum time limit. My question is, how am i going to count every 1000 lines in file then post to server and repeat same step again by using function in order not to exceed the default time. For example, my file has 20,000 records. I know there is some other methods like set time limit function, change time limit in php.ini....but i'm not going to do that as it will mess up the server. Any expert here can help me out? Thanks for looking this up for me :D

Here is my original code tat result Max Time Limit fatal error:

$handle = fopen("test.csv", "r");
fgetcsv($handle, 20000, ",");

while (($data = fgetcsv($handle, 20000, ",")) !== FALSE){
$user = str_replace(",", "", $data[0]);
$userid = str_replace(",", "", $data[1]);
$pwd = str_replace(",", "", $data[2]);
$email = str_replace(",", "", $data[3]);

$query ="UPDATE testdb SET user='".$user."',userid='".$userid."',pwd='".$pwd."',
email='".$email."' WHERE user='".$user."' LIMIT 1";
echo $query."<hr>";
mysql_query($query) or die(mysql_error());

}
fclose($handle);

dublinmike

10:26 am on Jun 18, 2008 (gmt 0)

10+ Year Member



Hi there,

You mighn't have to change anything in your php.ini file, put:


ini_set("memory_limit","32M");
set_time_limit(0);

at the top of your file and give it a try. That will attempt to change those environment variables for just this script. It won't affect any external scripts.

henry0

11:30 am on Jun 18, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You might consider a "sort of pseudo cron" by using SLEEP() [php.net]
then do a loop (including the sleep) to update by (for ex:) 1000 at a time, do a pause and increase $i by 1000
etc....

benghee

2:03 am on Jun 19, 2008 (gmt 0)

10+ Year Member



Thanks for your reply, henry0. I'm not quite experienced in complicated coding, can you show me any example that using your method. Thanks again :)

benghee

8:32 am on Jun 23, 2008 (gmt 0)

10+ Year Member



Ok, here i rewrite the code to include the sleep() function. All the records can be displayed without any timeout but when they update to MYSQL, it takes too long and result Max time limit error no matter how long I'm gonna set the limit. Any idea how to solve it? Thanks.

<?php

set_time_limit(1200);
$send_count = 1000;
$send_delay = 1;

$handle = fopen("2006.csv", "r");
fgetcsv($handle, 20000, ",");

while (($data = fgetcsv($handle, 20000, ",")) !== FALSE){

if($send_count == 0)
{
$send_count = 10;
sleep($send_delay);
}
$send_count--;

$user = str_replace(",", "", $data[0]);
$userid = str_replace(",", "", $data[1]);
$pwd = str_replace(",", "", $data[2]);
$email = str_replace(",", "", $data[3]);

$query ="UPDATE testdb SET user ='".$user."',userid='".$userid."',pwd='".$pwd."' ,
email='".$email."' WHERE user='".$user."' LIMIT 1";
echo $query."<hr>";

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

}
fclose($handle);

?>