Maximum execution time of 30 seconds exceeded in - php

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;
}
}
?>``

Related

Loop in PHP taking time, Fatal error: Maximum execution time of 120 seconds exceeded in

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--;
}

Memory Exhausting in mysql_free_result()

I made a method on my class to fetch and store in a array all the results the desired SQL statement has in it, and it works just fine. Now, after some months in production, I came across this error:
Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 3503272 bytes) in C:\xxpathxx\class.DGC_Gerais.php on line 0
As I begin to inspect, I tracked the error to the mysql_free_result(), but upon commenting that line, it still doenst work.
Here's the _fetch method:
PUBLIC static function _fetch($sql){
$q = mysql_query($sql);
if(mysql_num_rows($q) > 0):
for($a=0 ; $linha = mysql_fetch_row($q) ; $a++){ // forEach row.
foreach($linha as $chave => $valor): // forEach field.
$result[$a][$chave] = $valor;
endforeach;
}
// mysql_free_result($q);
return $result;
else:
return false;
endif;
}
That code is extremely convoluted and can be simplified to:
public static function _fetch($sql) {
$q = mysql_query($sql);
if (mysql_num_rows($q) == 0) {
return false;
}
$result = array();
while ($linha = mysql_fetch_row($q)) {
$result[] = $linha;
}
return $result;
}
Does exactly the same without double loops.
The problem is that you're fetching all that data from the database and are storing it in $result, which means it needs to be stored in memory. And PHP limits the amount of memory available to scripts by default, so you're simply exceeding that limit. mysql_free_result has nothing as such to do with it. First try to fetch less data, or to process the data inside that while loop without needing to store everything in an array.
If that doesn't help, carefully turn up the memory limit with ini_set('memory_limit', $limit).

Check if entry exists with mySQL / PHP without getting timeout?

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';
}
};

Ajax request max execution timing out

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.

PHP problem with die()

So, I have this code:
} else {
$photograph_moderation = new PhotographModeration($this->photograph_id);
$photograph_moderation->purgePhotograph();
//eventually take to an error page
die('image is not big enough to upload');
}
the purgePhotograph() function gets called properly when this condition is met, but the script never appears to die. Is there a reason why die wouldn't get called here? purgePhotograph() has no script killing commands either.
Here's the purge_photograph function:
public function purgePhotograph() {
$db = Connect::connect();
$photograph_id = $db->real_escape_string($this->photograph_id);
$query = "SELECT * from photographs WHERE id='{$this->photograph_id}'";
$result = $db->query($query);
$photograph = $result->fetch_assoc();
if ($photograph['location'])
unlink($photograph['location']);
if ($photograph['thumbnail_location'])
unlink($photograph['thumbnail_location']);
if ($photograph['watermark_location'])
unlink($photograph['watermark_location']);
if ($photograph['xsmall_location'])
unlink($photograph['xsmall_location']);
if ($photograph['small_location'])
unlink($photograph['small_location']);
if ($photograph['medium_location'])
unlink($photograph['medium_location']);
if ($photograph['large_location'])
unlink($photograph['large_location']);
if ($photograph['xlarge_location'])
unlink($photograph['xlarge_location']);
if ($photograph['xxlarge_location'])
unlink($photograph['xxlarge_location']);
if ($photograph['xxxlarge_location'])
unlink($photograph['xxxlarge_location']);
$query = "DELETE from photographs WHERE id='{$this->photograph_id}'";
$result = $db->query($query);
$query = "DELETE from photograph_tags WHERE photograph_id='{$this->photograph_id}'";
$result = $db->query($query);
}
Check if purgePhotograph() returns. Maybe it has a deadloop or takes really long time.
Maybe now is the time to install a php debugger module and step into the code in question.
xdebug and e.g. netbeans as the frontend work well enough.
Wow, the problem was purgePhotograph() never had a return 1; at the end. I didn't know this was required for following lines to execute.
Try to put it into an try/catch block.
Maybe something is throwing an exception before die can get executed.
Are you getting any error?

Categories