How do I set the TimeOut for EXEC the Query or Stored Procedure in SqlSrv PHP
Because i am using PHP to call SQL-Server Stored Procedure.
For ex
"EXEC SP_Name"
some times its taking too long time, so the PHP page shows 500 Internal server error.
If possible to set the time limit the SP was stopped then i show the error description etc...
how do i fix this issue?
Thanks in Advance.
in your php.ini file set
max_execution_time = 60--or whatever time you wish
Refer This Link for more details
The #Jayasurya Satheesh's answer refers to the time of any php execution time, but not for db query exec time. Actually php lacks in a easy way to control the execution time of a db query.
It is more on the DB/SQL settings side. If you can define the TimeOut on the SQL Server side, this will return "timeout" to PHP.
Anyway, you can define set_time_limit(25) (where 25 is the number of seconds you want), You can put this in your php code before your SQL EXEC, not neccesary to deal with php.ini if you want to set_time_limit for specific php file. So, if the query takes more than 25 seconds, the php will stop execution.
Remember set_time_limit() does not affect other php scritps, it's only for the current file where it's placed
Related
I am developing a web service where I need to call an Oracle procedure in PHP. The Oracle procedure will take time to process and after completion it will write in a table.
How do I return an error in the web service response if the procedure call is taking too much time?
Note: I am stuck with PHP 5.2 and I cannot install cURL.
Did you consider using http://php.net/manual/en/function.set-time-limit.php ?
Sets the number of seconds a script is allowed to run. If this is reached, the script returns a fatal error. The default limit is 30 seconds or, if it exists, the max_execution_time value defined in the php.ini.
You can set it for your specific script for less than 30s, so it stops after e.g. a few seconds. And then you just handle empty response / error response.
In php.ini default execution time limit is 30 seconds... Customise it & write your code in try catch & handle exception with customise error message.
I have it set up in my PHP.ini file to timeout after 30 seconds. I want to keep this setting. I have a script that connects to an API and every once in a while (like 1 in 10 times), it can't connect so it spends the entire 30 seconds. This connection is via curl.
I have a logs table in my database and I would like before the timeout occurs to insert a new item. Is this possible via PHP to insert a row on timeout for only this particular script?
I have done my own research and can't find anything. Any help would be appreciated.
You can use register_shutdown_function to execute something on script shutdown. It will even be called on script timeout.
edit:
Alternatively you can use CURLOPT_TIMEOUT to set the max execution time for your curl execution to something below 30s and then look at the returned value to see if max execution was reached and then log to the database.
I've got a command-line PHP script that connects to SQL Server 2005 using an ODBC connection. It uses odbc_exec() to execute a stored procedure which may take a while to process, and I don't want it to timeout.
I've been unable to find anything in the PHP documentation regarding how to set the timeout for odbc_exec(). Does it default to an infinite wait?
There's only so much you can do here. PHP's default script execution time is set to 30 seconds. There are numerous ways to change this, including the set_time_limit() function (found in the manual), and extending execution time by using ini_set, or using odbc_setoption
//extend execution time
$num_minutes = 5;
ini_set('max_execution_time', (60*$num_minutes)); //set timeout to 5 minutes
You can also change this setting inside your php.ini file. However, all of these only control the scope within what your apache server can control. If the database itself has a timeout, it will still cut off your call and return an error, even if your personally set execution times are elevated.
According to php.net you can use odbc_setoption():
$result = odbc_prepare($conn, $sql);
odbc_setoption($result, 2, 0, 30);// 30 seconds timeout
odbc_execute($result);
So you can increase timeout to some bigger number depending on your need
I have an upload form that uploads mp3s to my site. I have some intermittent issues with some users which I suspect to be slow upload connections...
But anyway the first line of code is set_time_limit(0); which did fix it for SOME users that had connections that were taking a while to upload, but some are still getting timed out and I have no idea why.
It says the script has exceeded limit execution of 60 seconds. The script has no loops so it's not like it's some kind of infinite loop.
The weird thing is that no matter what line of code is in the first line it will always say "error on line one, two, etc" even if it's set_time_limit(0);. I tried erasing it and the very first line of code always seems to be the error, it doesn't even give me a hint of why it can't execute the php page.
This is an issue only few users are experiencing and no one else seems to be affected. Could anyone throw some ideas as to why this could be happening?
set_time_limt() will only effect the actual execution of the PHP code on the page. You want to set the PHP directive max_input_time, which controls how long the script will accept input (like files) for. The catch is that you need to set this in php.ini, as if the default max_input_time is exceeded, it'll never reach the script which is attempting to change it with ini_set().
Sure, a couple of things noted in the PHP Manual.
Make sure PHP is not running in safe-mode. set_time_limit has no affect when PHP is running in safe_mode.
Second, and this is where I assume your problem lies.....
Note: The set_time_limit() function and the configuration directive max_execution_time only affect the execution time of the script itself. Any time spent on activity that happens outside the execution of the script such as system calls using system(), stream operations, database queries, etc. is not included when determining the maximum time that the script has been running. This is not true on Windows where the measured time is real.
So your stream may be the culprit.
Can you post a little of your upload script, are you calling a separate file to handle the upload using Headers?
Try ini_set('max_execution_time', 0); instead.
I have an update query being run by a cron task that's timing out. The query takes, on average, five minutes to execute when executed in navicat.
The code looks roughly like this. It's quite simple:
// $db is a mysqli link
set_time_limit (0); // should keep the script from timing out
$query = "SLOW QUERY";
$result = $db->query($query);
if (!$result)
echo "error";
Even though the script shouldn't timeout, the time spent waiting on the sql call still seems to be subject to a timeout.
Is there an asynchronous call that can be used? Or adjust the timeout?
Is the timeout different because it's being called from the command line rather than through Apache?
Thanks
I had the same problem somwhere, and "solved" it with the following code (first two lines of my file):
set_time_limit(0);
ignore_user_abort(1);
According to the manual:
Note: The set_time_limit() function and the configuration directive max_execution_time only affect the execution time of the script itself. Any time spent on activity that happens outside the execution of the script such as system calls using system(), stream operations, database queries, etc. is not included when determining the maximum time that the script has been running.
So it's unlikely to have anything to do with PHP's time limit. What message are you getting when it times out? Perhaps there's a MySQL setting involved.
Is your php running in safe-mode? Quote from PHP manual of set_time_limit:
This function has no effect when PHP
is running in safe mode. There is no
workaround other than turning off safe
mode or changing the time limit in the
php.ini.
I came across something similar in one of my PHP scripts, I added this inline right before executing the slow query:
$timeout_seconds = 3153600; // 1 year...
// Make sure the PHP script doesn't time out
set_time_limit(0);
ignore_user_abort(1);
// Make sure the PHP socket doesn't time out
ini_set('default_socket_timeout', $timeout_seconds);
ini_set('mysqlnd.net_read_timeout', $timeout_seconds);
// Make sure the MySQL server doesn't time out
// Assuming your $link is a MySQLi object:
$link->query("SET SESSION connect_timeout=" . $timeout_seconds);
$link->query("SET SESSION delayed_insert_timeout=" . $timeout_seconds);
$link->query("SET SESSION have_statement_timeout='NO'");
$link->query("SET SESSION net_read_timeout=" . $timeout_seconds);
$link->query("SET SESSION net_write_timeout=" . $timeout_seconds);
Obviously, you'll want to set the seconds appropriately, but I didn't want my script to time out at all.
PHP Init directives: https://www.php.net/manual/en/ini.list.php
MySQL Server variables: https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html
Note: Check the documents for more information, and also verify the variables match the PHP and MySQL versions that you are using. I'm using PHP 7.3 and MySQL 5.7.
** Edit: Setting the PHP timeouts wasn't enough for my script, I had to add the MySQL SESSION variables too.
Assuming you are on linux, Debian based systems have separate configurations for mod_php/php cgi and php-cli. This shouldn't be too difficult to set up on a different linux system that doesn't separate cgi/cli configuration.
Once you have separate configs, I would adjust your php cli configuration. Disable safe mode and any time limits and ram limits.
Check out some of the resource limit variables in php,ini:
max_execution_time, max_input_time, memory_limit
You could also set a time limit for the script in PHP itself:
http://ca3.php.net/set_time_limit