0

我正在编写一个模块,该模块从我们的分销商网站下载产品数据,然后根据我定义的设置对其进行分类。下SQL表定义了类别映射:

在此处输入图像描述

这些函数是从一个foreach也下载数据的循环中调用的。在编写查询上述数据库并根据从我们的经销商处收到的字符串值保存类别的函数之前,我没有收到任何错误。这是我调用该函数的行:

//Get PS category based on mapped values
$PsCategory = getPsCategory($category1);

调用此函数后,我开始收到以下错误:

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

相关功能如下:

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

您可能会注意到,我将其分为两个功能,一个作为控件,一个仅用于SQL查询。最初,它们都在同一个功能中,但我已将其分开以帮助进行故障排除。我的数据库文件如下:

$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"

第 7 行在这里:

$db = new PDO($dsn, $username, $password);

据我所知,我似乎在代码的某处进入了无限循环。使用此 db 文件时,没有其他函数有任何错误,而且我的查询语法中看不到任何错误。数据库不是太大,因为它只包含 9 条记录。

如果有人能指出我错过的东西,我将不胜感激。


当我执行 print_r 时,我得到以下输出:

 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 ) ) 
4

1 回答 1

1

似乎连接到您的数据库需要很长时间。这以前有效吗?

您可以尝试以下方法来延长脚本可以运行的时间:

ini_set('max_execution_time', 600); //600 seconds = 10 minutes

似乎连接的时间太长了。您使用 ODBC,对吗?

更新:

我看到你的包括

include('C:/xampp/htdocs/prestaprep1-5-3-1/modules/stlimportmodule/model/database.php'); 

一个函数内。这意味着,如果您有 for 循环,您可能会创建许多 $db 对象并导致冻结。您需要将包含移出函数,并使用全局 $db 变量。

于 2013-04-19T18:32:04.120 回答