doubi1713 2015-02-03 16:51
浏览 67

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

I am using the following multi curl actions to check the status of various web pages. The function first gets various users and then for each user does a curl multi request checking various web pages. It seems to work okay except that occasionally the db->execute function seems to fail for all but 1 user. The function only adds one set of records and ignores the remaining arrays from the other users.

Can anyone see why this might be happening?

// 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条回答 默认 最新

  • dtcrw26206 2015-02-03 16:58
    关注

    As I understand, the class

    $db = GetDb();
    

    Is using a design pattern called Singleton which creates an instance ONCE and it overrides the previous calls. To fix it, DO NOT use singleton when you try to connect with PDO\mysqli. Create a connection and make a new variable for it each time you want to make a call to the database.

    评论

报告相同问题?

悬赏问题

  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)