0

I am designing custom query page where user can fire query and get the desired result. In my database I have crores of records obviously it takes time around 10 to 15 minutes to execute . After timeout page gets terminated and code below the php script is not executed so it is not showing below design and footer.

Please let me know how to handle timeout properly so that whole page executes and gives proper user defined timeout message and also below design and footer if execution time exceeds.

Here is what I am trying to do...

 <form onsubmit="custom.php;" id="usrform" method="post">
     <textarea id="tarea" placeholder=" Example: select  malindex,malname,virustype,filetype,count from CT_DETECTIONS;" name="query" form="usrform" style="width:800px;height:100px;"></textarea>
   <br>
   <br><input type="submit" value="Submit Query" onclick="validate();">
</form> 
<br>  
<?php

//DB connection

if(isset($_POST['query'])&& $_POST['query']!='')
{
$x = $_POST['query']; 
$result = mysqli_query($con,$x);
if(!$result)
{ 
$msg='<b>Error: </b>Invalid query. Enter a valid query. Please refer to Database Schema <a href="tableinfo.html"><b style="color:black;">here</b> </a>';
}
else
{
$x="<b>Query: </b>" .$_POST['query'];
//fetching rows and drawing google chart table    

<?php echo $x;
} 
}?>
<div id="valid" style="color:red;">
  <?php echo $msg; ?>
</div> 
<div style="color:red">
  <lable id="myP">   </lable>
</div>
</div>
</div>

<div id="visualization" style="width:800px; height: 450px;color:black;margin-left:22px;"></div>
</div>
</div>

Please let me know how to handle page termination due to timeout

Jerodev
  • 32,252
  • 11
  • 87
  • 108
Rajendra
  • 373
  • 1
  • 2
  • 18
  • You can try AJAX to execute query. The page with header and footer will be already loaded, on ajax request error handler you can display the error message to the user. – Damien Sep 25 '14 at 07:38
  • I am not much aware of AJAX.Could you please let me know how to do it? – Rajendra Sep 25 '14 at 08:26

3 Answers3

0

You could either change the time limit by using set_time_limit(60*60); www.php.net which will set it to 1h or take a look on this article on Stackoverflow.

Community
  • 1
  • 1
ThatMSG
  • 1,456
  • 1
  • 16
  • 27
  • Increase timeout is not my solution.I want to handle page termination properly and want to load whole page – Rajendra Sep 25 '14 at 08:27
  • That is what I thought and why I gave you an other Link where some one ask a similar question on stackoverflow :) http://stackoverflow.com/questions/6861033/how-to-catch-the-fatal-error-maximum-execution-time-of-30-seconds-exceeded-in-p they provide a whole lot of possible solutions – ThatMSG Sep 25 '14 at 08:42
0

You can check for the maximum execution time value from this script:

if(ini_get('max_execution_time')</*Set value do you want(integer)*/{
//Overlimit
}
else{
//Your script
}
Hendry Tanaka
  • 454
  • 3
  • 11
  • time increament is not the solution .I want to handle the page termination due to timeout – Rajendra Sep 25 '14 at 08:27
  • The code above is not about increase the maximum execute time in php.ini but to get the value from maximum execution in php.ini file in your php page. Try to echo ini_get('max_execution_time') and what do you get? – Hendry Tanaka Sep 25 '14 at 08:39
0

Using AJAX :

<?php

//DB connection

if(isset($_POST['query']))
{
    // MUST BE FIRST OUTPUT (nothing before)
    header('Content-type: text;charset=UTF-8');

    $x = $_POST['query'];
    $result = mysqli_query($con,mysqli_real_escape_string($con, $x));
    if (!$result)
    { 
        echo '<b>Error: </b>Invalid query. Enter a valid query. Please refer to Database Schema <a href="tableinfo.html"><b style="color:black;">here</b> </a>';
        exit();
    }

    //fetching rows and drawing google chart table    

    echo $x;
    exit();
}
?>

<form onsubmit="return submitUsrForm();" id="usrform" method="post">
     <textarea id="tarea" placeholder=" Example: select  malindex,malname,virustype,filetype,count from CT_DETECTIONS;" name="query" form="usrform" style="width:800px;height:100px;"></textarea>
   <br>
   <br><input type="submit" value="Submit Query" onclick="validate();">
</form> 
<br>
<b>Query: </b><span id="query"></span>
<div id="valid" style="color:red;">
</div> 
<div style="color:red">
  <lable id="myP">   </lable>
</div>
</div>
</div>

<div id="visualization" style="width:800px; height: 450px;color:black;margin-left:22px;"></div>
</div>
</div>

<script type="text/javascript">
function validate() { return true; }
function submitUsrForm() {
    // clear content between each query
    document.getElementById("valid").innerHTML = "";
    document.getElementById("visualization").innerHTML = "";
    // get textarea content
    var query = document.getElementById("tarea").value;
    // Create AJAX request
    if (window.XMLHttpRequest) {
        var xhr = new XMLHttpRequest();
    } else {
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xhr.open("POST", "custom.php", true);
    xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xhr.send("query=" + encodeURI(query));
    xhr.onreadystatechange=function() {
        if (xhr.readyState == 4) {
            if (xhr.status == 200) {
                // success
                document.getElementById("visualization").innerHTML = xhr.responseText;
            }
            else {
                // error
                document.getElementById("valid").innerHTML = xhr.responseText;
            }
        }
    }

    return false;
}
</script>

To test execution timeout you can add after header :

ini_set('max_execution_time',1 ); 
sleep(3); 
Damien
  • 644
  • 4
  • 11