samfisher753
samfisher753

Reputation: 21

PHP script slow on server, but fast in local

First I want to say that my english is very poor, but I'm going to try.

I've tried to run a PHP script on my PC with wamp server and it works ok, but when I upload, for some reason, it spends so much time to complete the execution on the host, and almost always ends with a Service Temporarily Unavailable error (the host close the connection).

I've used some die() to see where is the problem and I found it's a for loop where I'm making a big string (I'm only concatenating to make a big INSERT for executing it after the loop). And this loop works in local... I can't understand why is not working on the host.

//insertar valores en bbdd
$sql = "Insert into valor values ";
$primer = 1;

$tiempo_inicio = microtime(true);

for($i = 0 ; $i <= count($array2) - 1 ; $i++)
{   

    //insertar glucosa
    if(!$array2[$i][1] == "")
    {
        if (!$primer) $sql .= ", "; 
        else $primer = 0;

        $sql .= "('" . $this->paciente . "', '" . $array2[$i][0] . "', 'Glucosa', " . $array2[$i][1] . ")";

        $this->Comprobar_Aumentar_Avisos($array2[$i][0], $array2[$i][1]);
    }

    //insertar raciones
    if(!$array2[$i][2] == "")
    {
        if (!$primer) $sql .= ", "; 
        else $primer = 0;

        $sql .= "('" . $this->paciente . "', '" . $array2[$i][0] . "', 'Raciones', " . $array2[$i][2] . ")";

    }

    //insertar insulina
    if(!$array2[$i][3] == "")
    {
        if (!$primer) $sql .= ", "; 
        else $primer = 0;

        $sql .= "('" . $this->paciente . "', '" . $array2[$i][0] . "', 'Insulina', " . $array2[$i][3] . ")";    
    }
}

$tiempo_total = microtime(true) - $tiempo_inicio;

die($tiempo_total);


if ($sql != "Insert into valor values ") {
    $AccessBD = new TAccessBD;
    $AccessBD->usuario = $this->paciente;
    $AccessBD->Inicialitzar_BD();
    $AccessBD->query = $sql;
    $res = $AccessBD->Ejecutar_SQL();   
    $AccessBD->Finalitzar_BD();
    unset($AccessBD);
}

Upvotes: 1

Views: 1383

Answers (1)

samfisher753
samfisher753

Reputation: 21

The problem was that in the loop I was accessing too many times the database in this function: $this->Comprobar_Aumentar_Avisos($array2[$i][0], $array2[$i][1]);

When accessing in local I have no problem, because of local database, but on the server, with the database in other external host too, it was very slow.

So I solved it reducing the number of accesses to database to the minimum I can and now it's working fine.

Thank you very much for the help!

Upvotes: 1

Related Questions