0

I am not sure why but my MySQL query below, makes my page not respond and I get that box saying this page has became unresponsive. I am wondering what would be the easiest way to make sure that this does not happen.

public function audioplayer($id)
{
    $r_hostname = "192.***.**.***";
    $r_username = "c**";
    $r_password = "*******";
    $link = mysql_connect($r_hostname,$r_username,$r_password);

    $a_hostname = "192.168.***.***";


    $db = mysql_select_db('asterisk', $link);

    $result = mysql_query("SELECT * FROM recording_log WHERE start_time LIKE '".date("Y-m-d")."%' AND filename LIKE 'IL_%-%". $id ."' LIMIT 3",$link);


    #$result = mysql_query("select * from recording_log WHERE filename LIKE 'IL_%-%".$id."'",$link);

    if (!$result) {
            die('Invalid query: ' . mysql_error());
    }   
    if($result != '')
    {
        while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
            foreach($row as $column => $value) {
                $array[type] ="wav";    
                $array[$column]= $value;
            }
        }
    }
    else {
        $linktwo = mysql_connect($a_hostname,$r_username,$r_password);

        $dbtwo = mysql_select_db('asterisk', $linktwo);

        $resulttwo = mysql_query("SELECT * FROM recording_log WHERE start_time LIKE '".date("Y-m-d")."%' AND filename LIKE 'IL_%-%". $id ."' LIMIT 3",$linktwo);

        while($row = mysql_fetch_array($resulttwo, MYSQL_ASSOC)){
            foreach($row as $column => $value) {
                $array[type] ="mp3";
                $array[$column]= $value;
            }
        }
    }
    return json_encode($array);
}
George Cummins
  • 28,485
  • 8
  • 71
  • 90
RussellHarrower
  • 6,470
  • 21
  • 102
  • 204
  • Are you sure that the query is causing the problem? Have you tried running the query against the database directly? If so, how long did it take, and how does that time compare with your PHP _max_execution_time_ configuration value? – George Cummins Jun 11 '13 at 01:57

1 Answers1

1

Feels like there's a syntax error here:

$array[type] ="mp3";

Did you mean:

$array[$type] ="mp3";
$array["type"] ="mp3";

Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
  • 1
    @RussellHarrower Even `for ($i = 0; $a < $b; $s++)` is right. Syntactically right. Not logically right! – Praveen Kumar Purushothaman Jun 11 '13 at 01:59
  • 1. You might wanted to use `$type` but accidentally used `type`. 2. You might wanted a string `type` for the index, but accidentally used `type`. – Praveen Kumar Purushothaman Jun 11 '13 at 02:02
  • I am downvoting for two reasons. First, the original answer is an issue, but doesn't really address the slowness that was the OP's focus. Second (and more importantly) the edit added by @NullPoiиteя is spammy and unrelated. This does not address the problem of unresponsiveness. At _best_, that edit should be a comment. Even better, NullPoiиteя should note that the information has been added to the tag wiki and does not need to be repeated on every `mysql_*` question. – George Cummins Jun 11 '13 at 02:12
  • 1
    @GeorgeCummins ___spammy and unrelated___. really??? really ???.... see code is question again ..... and its perfectly fine to tell about deprecated api – NullPoiиteя Jun 11 '13 at 02:15
  • @GeorgeCummins This is bad. – Praveen Kumar Purushothaman Jun 11 '13 at 02:16
  • @GeorgeCummins if there is `mysql_*` in code in question then there is nothing wrong to add ...and i am done – NullPoiиteя Jun 11 '13 at 02:16