简体   繁体   English

为什么用PHP执行SQL查询时会收到超时错误?

[英]Why am I receiving a time out error while performing a SQL query with PHP?

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: 类别映射在以下SQL表中定义:

在此处输入图片说明

The functions are called from within a foreach loop that also downloads the data. foreach循环中调用这些函数,该循环还下载数据。 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. 您可能会注意到,我将其分为两个函数,一个作为控件,另一个仅用于执行SQL查询。 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: 第7行在这里:

$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. 该数据库不是太大,因为它仅包含9条记录。

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: 当我执行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 ) ) 

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? 您使用ODBC,对吗?

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. 这意味着,如果有for循环,则可能会创建许多$ db对象并导致冻结。 You need to move the include out of the function, and use a global $db variable. 您需要将include移出函数,并使用全局$ db变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM