0

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);
user1098178
  • 697
  • 3
  • 9
  • 25

1 Answers1

0

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.

Yair.R
  • 795
  • 4
  • 11