Reputation: 45
I am writing a module that downloads product data from our distributor's site and then categorizes it based on settings that I have defined. The category mappings are defined in the following SQL
table:
The functions are called from within a foreach
loop that also downloads the data. I did not receive any errors prior to writing the function that queries the above database and saves the category based on a string value received from our distributor. Here is the line where I call the function:
//Get PS category based on mapped values
$PsCategory = getPsCategory($category1);
After calling this function, I started getting the following error:
Fatal error: Maximum execution time of 30 seconds exceeded in C:\xampp\htdocs\prestaprep1-5-3-1\modules\stlimportmodule\model\database.php on line 7
The relevant functions are below:
function getMappedData(){
include('C:/xampp/htdocs/prestaprep1-5-3-1/modules/stlimportmodule/model/database.php');
$query='SELECT * FROM category_maps';
$statement = $db->prepare($query);
$statement -> execute();
$fetchedData = $statement ->fetchAll();
$statement -> closeCursor();
return $fetchedData;
}
function getPsCategory($stlCategory){
$fetchedData = getMappedData();
// print_r($fetchedData);
foreach ($fetchedData as $mappedSTLvalues) {
if($stlCategory == $mappedSTLvalues[0]){
$psCategoryValue = $mappedSTLvalues['ps_category'];
}else{
$psCategoryValue = 2;
}
}
return $psCategoryValue;
}
You may notice that I have this divided into two functions, one as a control and one just to do the SQL
query. Initially, it was all in the same function but I have divided it up to help with troubleshooting. My db file is as follows:
$dsn = 'mysql:host=localhost;dbname=ps_development';
$username = 'root';
$password = '';
try {
$db = new PDO($dsn, $username, $password);
} catch (PDOException $e) {
$error_message = $e->getMessage();
include('database_error.php');
exit();
}
//DEBUG: echo "database.php is referenced"
Line 7 is here:
$db = new PDO($dsn, $username, $password);
As best as I can tell, it appears that I am entering an infinite loop somewhere in my code. No other functions have any errors when using this db file and I can't see any errors in my query syntax. The db isn't too large since it only contains 9 records.
If anyone can point something out that I have missed, I would appreciate it.
When I do a print_r, I get the following output:
Array ( [0] => Array ( [stl_category] => Christian Living [0] => Christian Living [ps_category] => 38 [1] => 38 [id] => 3 [2] => 3 ) [1] => Array ( [stl_category] => Inspirational Motivation [0] => Inspirational Motivation [ps_category] => 230 [1] => 230 [id] => 4 [2] => 4 ) [2] => Array ( [stl_category] => Love and Marriage [0] => Love and Marriage [ps_category] => 231 [1] => 231 [id] => 5 [2] => 5 ) ) Array ( [0] => Array ( [stl_category] => Christian Living [0] => Christian Living [ps_category] => 38 [1] => 38 [id] => 3 [2] => 3 ) [1] => Array ( [stl_category] => Inspirational Motivation [0] => Inspirational Motivation [ps_category] => 230 [1] => 230 [id] => 4 [2] => 4 ) [2] => Array ( [stl_category] => Love and Marriage [0] => Love and Marriage [ps_category] => 231 [1] => 231 [id] => 5 [2] => 5 ) ) Array ( [0] => Array ( [stl_category] => Christian Living [0] => Christian Living [ps_category] => 38 [1] => 38 [id] => 3 [2] => 3 ) [1] => Array ( [stl_category] => Inspirational Motivation [0] => Inspirational Motivation [ps_category] => 230 [1] => 230 [id] => 4 [2] => 4 ) [2] => Array ( [stl_category] => Love and Marriage [0] => Love and Marriage [ps_category] => 231 [1] => 231 [id] => 5 [2] => 5 ) ) Array ( [0] => Array ( [stl_category] => Christian Living [0] => Christian Living [ps_category] => 38 [1] => 38 [id] => 3 [2] => 3 ) [1] => Array ( [stl_category] => Inspirational Motivation [0] => Inspirational Motivation [ps_category] => 230 [1] => 230 [id] => 4 [2] => 4 ) [2] => Array ( [stl_category] => Love and Marriage [0] => Love and Marriage [ps_category] => 231 [1] => 231 [id] => 5 [2] => 5 ) ) Array ( [0] => Array ( [stl_category] => Christian Living [0] => Christian Living [ps_category] => 38 [1] => 38 [id] => 3 [2] => 3 ) [1] => Array ( [stl_category] => Inspirational Motivation [0] => Inspirational Motivation [ps_category] => 230 [1] => 230 [id] => 4 [2] => 4 ) [2] => Array ( [stl_category] => Love and Marriage [0] => Love and Marriage [ps_category] => 231 [1] => 231 [id] => 5 [2] => 5 ) ) Array ( [0] => Array ( [stl_category] => Christian Living [0] => Christian Living [ps_category] => 38 [1] => 38 [id] => 3 [2] => 3 ) [1] => Array ( [stl_category] => Inspirational Motivation [0] => Inspirational Motivation [ps_category] => 230 [1] => 230 [id] => 4 [2] => 4 ) [2] => Array ( [stl_category] => Love and Marriage [0] => Love and Marriage [ps_category] => 231 [1] => 231 [id] => 5 [2] => 5 ) )
Upvotes: 0
Views: 1203
Reputation: 25725
Seems to be it is taking too long to connect to your database. Has this worked before?
You could try the following to extend the amount of time your script can run:
ini_set('max_execution_time', 600); //600 seconds = 10 minutes
It seems to be though that it is taking way too long to connect. Your using ODBC, correct?
Update:
I see your including
include('C:/xampp/htdocs/prestaprep1-5-3-1/modules/stlimportmodule/model/database.php');
within a function. This means, if you have for loops you may be creating many $db objects and causing the freeze. You need to move the include out of the function, and use a global $db variable.
Upvotes: 1