1

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);
}
5
  • did you mean the if statement to be like this !($array2[$i][1] == "") instead of !$array2[$i][1] == "" Commented Jun 22, 2013 at 16:18
  • You probably isolated the wrong code, see Remote Debugging and Memory Profiling in PHP. Also, prepared statements are speedier (and simpler) than manual INSERT value list concatenation. Commented Jun 22, 2013 at 16:19
  • how many entries in $array2? Commented Jun 22, 2013 at 16:20
  • It's almost difficult to reply to your question. My suggestion is to insert into the code some debugging info like... echo time()." Start processing value ".$i."\n"; echo time()." End processing value ".$i."\n"; end then try to figure out where bottle neck is. Some providers are limiting the database connection speed available on certain conditions (too much query in few minutes, allowed banda connection overquota, ...) Let us know! Commented Jun 22, 2013 at 16:21
  • @StefanoRadaelli Yes, I've put things like this. I've put die() before the loop and see that I get there, and then moved that die() after the loop to know when it finish and how much time it spends. In local I get to that die(), but on the server... I never get there, it's always loading until error appears. I will update a bit the code of my post to show that die(). Maybe I should use that prepared statements that mario says instead of concatenate. Commented Jun 22, 2013 at 16:37

1 Answer 1

1

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!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.