0

I am new to php and SQL.

I have a database with 45 000 rows, I need to extract all 'udid' attribute, so there is a lot of data. I put the result in an array, for each udid, I check if udid.xml exists, if the file exist i open it and search for some data, then i reorder the array..While this process I get a timeout error, when I call my script or a 504 error from my internet provider.

Here is my php code :

include("myFunctions.php"); 

header('Content-type: application/json; charset=utf-8');

$handle = fopen('php://input','r');

// ----------

set_time_limit (0);

$req_Push = $db->sql_query("SELECT * FROM utilisateurs"); 

echo("COUNT = " .$db->sql_numrows($req_Push). "");

$arrayBigTbl = array(); 

while ($row_Push = $db->sql_fetchrow($req_Push)){ // while udid ref fichier utilisateurs

    $idUser = $row_Push['udid'];

    $arrayBigTbl []= $idUser; 
}

//print_r($arrayBigTbl);

echo("</br>Count array global = " .count($arrayBigTbl). "--"); 

$arrayBigTbl = array_unique($arrayBigTbl); 

echo("</br>Count array global nettoyée = " .count($arrayBigTbl). " -- Fin2"); 

$arrayXMLDevice = array();


for ($i = 0; $i < count($arrayBigTbl) ; $i++)
{
    if (deviceXMLExist($arrayBigTbl[$i]))
    {
            $UDIDFile = simplexml_load_file("./UDID/".$arrayBigTbl[$i].".xml");             
            foreach($UDIDFile->UDID as $item)
            {
                $valueTemp = $item->valueUDID;
                $strig = "" .$valueTemp. ""; 
                $arrayXMLDevice[] = $strig; 
            }
    }
}

//print_r($arrayXMLDevice); 
echo("COUNT ARRAY DEVICE XML = " .count($arrayXMLDevice). " --- Fin 3"); 

$arrayXMLDevice = array_unique($arrayXMLDevice); 

echo("COUNT ARRAY DEVICE XML = " .count($arrayXMLDevice). " --- Fin 3"); 

$arrayXMLDevice = array_values($arrayXMLDevice);

$arraymerge = array_merge (  $arrayBigTbl,$arrayXMLDevice ); 
echo("COUNT ARRAY MERGE = " .count($arraymerge). " --- </br>"); 

$arraymerge = array_unique($arraymerge);

echo("COUNT ARRAY MERGE NETTOYEE  = " .count($arraymerge). " --- Fin 3"); 

$arrayFinalPush = array();

for ($i = 0; i < count($arraymerge);$i=$i+1)
{
    $aValue = $arraymerge[i]; 

    if(ctype_xdigit($deviceToken) && strlen($deviceToken)>60)
    {
        $arrayFinalPush[] = $aValue; 
    }
}

echo("COUNT ARRAY FINAL   = " .count($arrayFinalPush). " ---"); 

How can I resolve this problem so my request gets finished?

Aditya
  • 2,876
  • 4
  • 34
  • 30
  • this line of yours `$strig = "" .$valueTemp. "";`. What is this for? I've been seeing this sort of thing lately and I don't understand why this is done. You would think casting to string is to difficult: `$strig = (string)$valueTemp;` – hanzo2001 Dec 23 '13 at 11:26
  • No but i'm new to php, thank for the tip. – Jacques Attali Dec 23 '13 at 11:42

1 Answers1

0

I am not at all surprised you get a timeout error you are doing things very inefficiently.

That being said one trick I use with php is to flush the output buffer: How to flush output after each `echo` call?

If you flush the output buffer after each echo then it will give the browser some data and stop it from timing out.

The reason I say it is inefficient is because you generally want to do as much "work" as possible with SQL - things like uniqueness, sorting ordering etc. should be done in SQL rather than PHP whenever possible because SQL is built for it.

Community
  • 1
  • 1
ddoor
  • 5,819
  • 9
  • 34
  • 41
  • Thank you very much FaddishWorn, yea i know it is not efficient but it is my first and i'm in a hurry.Thank for the advice, i'll give a try. – Jacques Attali Dec 23 '13 at 10:28