While using this code to gather data using an Ajax XHTMLRequest I'm getting an PHP Max execution timeout
if(isset($_GET['func']) and $_GET['func'] == 'feed') {
global $ado;
$old_msg_id = $_GET['old_msg_id'];
$result = $ado->exec("SELECT * FROM `earnings` ORDER BY `id` DESC LIMIT 1");
while($row = $ado->fetch_assoc($result)) {
$last_msg_id = $row['id'];
}
while($last_msg_id <= $old_msg_id) {
$result = $ado->exec("SELECT * FROM `earnings` ORDER BY `id` DESC LIMIT 1");
while($row = $ado->fetch_assoc($result)) {
$last_msg_id = $row['id'];
}
}
$response = array();
$response['msg'] = 'new';
$response['old_msg_id'] = $last_msg_id;
echo json_encode($response);
}
The error I'm receiving in the error_log is
PHP Fatal error: Maximum execution time of 30 seconds exceeded in /ajax.php on line 165
Line 165 is the following :
while($last_msg_id <= $old_msg_id) {
I currently don't see a problem with the code, any hints as to what's wrong ?
JS level
In AJAX you can post the time out like below,
jQuery.ajax({
url: 'ajaxhandler.php',
success: function (result) {
returned_value=result;
},
timeout: 10000,
async: false
});
PHP level
If you are getting PHP you can change it in the PHP.ini
max_execution_time = 30 //make it like 300
Else in your PHP code use set_time_limit
When called, set_time_limit() restarts the timeout counter from zero.
In other words, if the timeout is the default 30 seconds, and 25
seconds into script execution a call such as set_time_limit(20) is
made, the script will run for a total of 45 seconds before timing out.
Also make sure that those while loops are not never ending loops.
It seems that your two while loops are taking too long to execute, Is there allot of data in the databases.
Related
Fatal error: Maximum execution time of 120 seconds exceeded in...
$level = "1";
$get_question = $user_home->runQuery('SELECT * FROM questions WHERE Level = :Level ORDER BY RAND()');
$get_question->bindparam(":Level",$level);
$get_question->execute();
$fetch_question=$get_question->fetch(PDO::FETCH_ASSOC);
$stmtpr = $user_home->runQuery("SELECT * FROM used WHERE Last=:user_name");
$stmtpr->execute(array(":user_name"=>$fetch_question['Id']));
$rowpr = $stmtpr->fetch(PDO::FETCH_ASSOC);
while($stmtpr->rowCount() > 0)
{
$get_questionl = $user_home->runQuery('SELECT * FROM questions WHERE Level = :Level ORDER BY RAND()');
$get_questionl->bindparam(":Level",$level);
$get_questionl->execute();
$fetch_question=$get_questionl->fetch(PDO::FETCH_ASSOC);
}
Execution takes time only when it goes in loop.
The value of $stmtpr->rowCount() never changes, so your loop never ends. You're basically saying:
while (10 > 1) { ... }
It's not exactly clear what you're trying to do, but it appears you're trying to re-query the same rows you just queried. You probably just want to loop over the original result set.
Also note that ORDER BY RAND() is notoriously non-performant. Check this question for some alternative ideas.
Add at the top this in your code
Unlimited execution time
ini_set('max_execution_time', 0);
Also, you can add unlimited memory usage
ini_set("memory_limit", "-1");
In your case also change the rowCount() like this
$count = $stmtpr->rowCount();
while($count > 0)
{
$get_questionl = $user_home->runQuery('SELECT * FROM questions WHERE Level = :Level ORDER BY RAND()');
$get_questionl->bindparam(":Level",$level);
$get_questionl->execute();
$fetch_question=$get_questionl->fetch(PDO::FETCH_ASSOC);
$count--;
}
I'm learning php MVC and in my display model i got this fatal error
Fatal error: Maximum execution time of 30 seconds exceeded in C:\xampp\htdocs\kowab\app\models\display.php on line 36
line 36 is $data = mysql_fetch_array($sql);
To remove this error you have to increase max_execution_time in your php.ini. Afterwards you have to restart the apache.
Or you add ini_set('max_execution_time', x) at the top of your script.
But you should think about optimizing your query and code first.
Are you watching from the Arabic man's tutorials? (Ali Hamdi)
I experienced the same thing and I made my else statement of the display class this way:
else
{
$num = mysql_num_rows($sql);
while ($num > 0)
{
$data = mysql_fetch_array($sql);
$num--;
}
}
return $data;
}
}
?>
It didn't solve the problem, but it brought back the form at least. So I continued watching the rest of the tutorials and following him so that later I address that part. I have written him and awaiting for his response. as soon as he does, I'll get back to you with the solution.
Up your execution time by making your first line of code:
set_time_limit($x);
$x should be the maximum time in seconds for running the script. A value of 0 will let the script run infinitely.
http://us1.php.net/set_time_limit
NOTE: It is weird that you hit a 30 second time limit on line 36, so you probably have a problem with your code that we can't identify, because you haven't posted it.
You can increase that time by looking for max_execution_time in php.ini but before that you need to know what cause this issue. Check your query there might be some loop or it returns a huge data
set_time_limit($seconds);
Per the docs. If you pass a value of 0 for $seconds there will be no time limit.
here is my model
// display
class display extends awebarts {
public function __construct($tablename) {
$this->tablename= $tablename;
$this->connectToDb();
$this->getData();
$this->close();
}
function getData() {
$query = "SELECT * FROM $this->tablename ORDER BY `ID` DESC LIMIT 1";
if(!$sql = mysql_query($query))
{
throw new Exception (mysql_error());
}
else {
$num= mysql_num_rows($sql);
while($num >0)
{
$data= mysql_fetch_array($sql);
}
}
return $data;
}
}
?>``
I keep getting PHP Fatal error: Maximum execution time of 30 seconds exceeded on the line here that checks if a mySQL entry exists. Even when I set a large max execution time it will have this error!
function checkReleasePosted($releaseID){
$result = mysql_query("SELECT * FROM `releases` WHERE `releaseID` = '$releaseID' LIMIT 1");
if(mysql_fetch_array($result) !== false){
return 'Assigned';
} else {
return 'Available';
}
};
Does anyone know of another way I can check the entry to avoid this error? (I am just learning with the old API and will change it to mysqli soon!). Any help would be greatly appreciated as I've been trying to get this to work for 2 days. Many thanks
You are possibly comparing a numeric column (releaseID) with a string value ('$releaseID'). This kills the index. Remove those quotes, if $releaseID is in fact a number and not a string.
This will make it easier
function checkReleasePosted($releaseID){
$result = mysql_query("SELECT * FROM `releases` WHERE `releaseID` = $releaseID LIMIT 1");
$num_rows = mysql_num_rows($result);
if($num_rows>0){
return 'Assigned';
} else {
return 'Available';
}
};
The following code makes the web loading for like 10 minutes and I dont know why!
function chatheartbeat(){
include("config.php");
$useradn = $_SESSION['displayname'];
$query = "select * from chat where (userbdn = '".$useradn."' AND isread = 1) order by id ASC";
$result = mysql_query($query , $link);
$num_rows = mysql_num_rows($result);
if ($num_rows >= 1) {
$items = array();
$i='1';
while($chat = mysql_fetch_array($result)){
$items[$i]['from']=$chat['useradn'];
$items[$i]['msg']=$chat['msg'];
$items[$i]['timee']=date("H:i" ,$chat['timee']);
$i++;
}
$query = "update chat set isread = 0 where userbdn = '".$useradn."' and isread = 1";
mysql_query($query , $link);
header('Content-type: application/json');
echo json_encode($items);
exit;
}else{
sleep(2);
chatheartbeat();
}
}
Any suggestions?
The function will never return until there are some results from the first select.
I would suggest that you return after n runs through (e.g. 5), even if there are no results. The client can then re-issue the ajax call to poll again.
Also, it's not a great idea to do this in a recursive manner. I would suggest doing this in a for loop instead.
You are calling your function recursively and as the session variable does not change between the function calls, if it goes to the else part the first time, it will go there every time, creating a never ending loop.
By the way, if you want to use polling, you should set a timer in the client part (javascript) and not use a recursive function on the server side.
There are a few things you want to look at when performing any long polling techniques.
You need to exit after some predefined time period. Waiting on the server until you have something to respond with will lead you to execution timeouts.
Know what is going on with your SESSION data. PHP by default will use file based sessions and locks the file during the course of the request (unless you intervine)
I have a custom script for a bulletin board system that counts the number of threads a user has made, and updates a column accordingly. This works fine, however with 100,000+ users, it times out when running it for the first time.
I've tried adding the following before the query, but it still times out (500 error).
set_time_limit(0);
ignore_user_abort(true);
Additional: I'm using this script on my vps.
Query:
set_time_limit(0);
ignore_user_abort(true);
$db->write_query("ALTER TABLE `".TABLE_PREFIX."users` ADD `numthreads` int(10) unsigned NOT NULL default '0'");
// load users into an array to count number of threads
$query = $db->simple_select("users", "uid");
while($user = $db->fetch_array($query))
{
$users[$user['uid']] = $user;
}
foreach($users as $user)
{
$query = $db->simple_select("threads", "COUNT(tid) AS threads", "uid = '{$user['uid']}'");
// get total number of threads
$numthreads = intval($db->fetch_field($query, "threads"));
$db->update_query("users", array("numthreads" => $numthreads), "uid = '{$user['uid']}'");
}
Use this:
ini_set('max_execution_time', 0);
Inplace of:
set_time_limit(0);
ignore_user_abort(true);
You can also edit php.ini:
max_execution_time = 60; //Maximum execution time of each script, in seconds
max_input_time = 60; //Maximum amount of time each script may spend parsing request data
Hope this helps.
First you should separate your ALTER statement. Execute the ALTER first and then do the rest. Alter table can be expensive in time if you have a big table. You can run it manually, using phpmyadmin or via shell (even better since there's no php timeout). which will give you the ability to not timeout.
Then remove the ALTER from the script and run it.
and then use:
$query = $db->simple_select("users", "uid");
while($user = $db->fetch_array($query))
{
$query = $db->simple_select("threads", "COUNT(tid) AS threads", "uid = '{$user['uid']}'");
$numthreads = intval($db->fetch_field($query, "threads"));
$db->update_query("users", array("numthreads" => $numthreads), "uid = '{$user['uid']}'");
}
try this
ini_alter ("max_execution_time", 600000000);
$tmp = ini_get ( "max_execution_time" );
set_time_limit(600000000);
A common reason that people are not able to change the max execution time is that their host does not allow them to. Make sure to find out that they do not block this.