PHP循环db执行多次有时会失败

时间:2015-02-03 16:51:39

标签: php sql curl curl-multi

我正在使用以下多个卷曲操作来检查各种网页的状态。该功能首先获得各种用户,然后为每个用户执行卷曲多请求检查各种网页。它似乎工作正常,除了有时除了1个用户之外,db->执行函数似乎都失败了。该函数仅添加一组记录,并忽略其他用户的其余数组。

有人能看出为什么会发生这种情况吗?

// open db
$db = GetDb();

// fetch user data
$query = 'SELECT * FROM userinfo ORDER BY user_id DESC';
$dbresult = $db->Execute($query);
$lists = array();
while ($dbresult && $row = $dbresult->FetchRow()) {
$one=array();
$user_id = $row['user_id'];

// create array
$data = array(array());

$data[0]['extra'] = array();
$data[0]['extra']['user_id']   = $user_id;
$data[0]['extra']['monitor_id']   = '2';
$data[0]['url']  = 'http://www.google.com';
$data[0]['post'] = 'google';

$data[1]['extra'] = array();
$data[1]['extra']['user_id']   = $user_id;
$data[1]['extra']['monitor_id']   = '6';
$data[1]['url']  = 'http://www.yahoo.com';
$data[1]['post'] = 'yahoo';

$data[2]['extra'] = array();
$data[2]['extra']['user_id']   = $user_id;
$data[2]['extra']['monitor_id']   = '4';
$data[2]['url']  = 'http://www.amazon.com';
$data[2]['post'] = 'amazon';

$data[3]['extra'] = array();
$data[3]['extra']['user_id']   = $user_id; 
$data[3]['extra']['monitor_id']   = '18';
$data[3]['url']  = 'http://www.bing.com';
$data[3]['post'] = 'bing';

$r = multiRequest($data);

echo '<pre>';
print_r($r);

$aValues = array();
foreach ($r as $aRow) {
    $aValues[] = "'" . implode("','", $aRow) . "'";
}
$values = "(" . implode("), (", $aValues) . ")";
$sQuery = "insert into `time_user_$user_id` (`user_id`, `monitor_id`, `monitor_type`, `monitor_name`, `response_code`, `user_message`, `create_date`, `string`, `status`) values $values";
$db->Execute($sQuery);

}

// close db
mysqli_close($db);

1 个答案:

答案 0 :(得分:0)

据我所知,班级

$db = GetDb();

使用名为Singleton的设计模式,它创建一个ONCE实例,它会覆盖以前的调用。 要解决此问题,请勿在尝试连接PDO \ mysqli时使用单例。每次要调用数据库时,都要为它创建一个连接并创建一个新变量。