I always have problems with PHP when doing large loops. I divided it into very small loops and made them hundreds of times instead, when it comes to large quantities.
This is one of the examples I'm working with now:
$stmt = $GLOBALS['link']->prepare('SELECT * FROM tracking_redirect');
$stmt->execute();
$tracking = new tracking();
while($click = $stmt->fetch()){
$tracking->checkClickIP($click['blogid'], $click['campaignid'], $click['id'], $click['ipaddress']);
}
tracking_redirect is a table with a lot of information about which user is being redirected to a site. I store data like ipaddress, cookie, date and user id. The table currently has ~ 60,000 rows.
checkClickIP () is a function for checking whether a click / redirect is valid or valid. Example to make sure that the same user does not click too many times.
public function checkClickIP($userid, $campaignid, $clickid, $ipaddress = null) {
if(!isset($ipaddress)){
$ipaddress = $_SERVER['REMOTE_ADDR'];
}
$datestmt = $GLOBALS['link']->prepare('SELECT crdate FROM tracking_redirect WHERE id=:clickid');
$datestmt->execute(array('clickid' => $clickid));
$datestmt = $datestmt->fetch();
$stmt = $GLOBALS['link']->prepare('SELECT COUNT(*) as total FROM tracking_redirect WHERE ipaddress=:ipaddress AND campaignid=:campaignid AND blogid=:userid AND crdate<:clickdate');
$stmt->execute(array(
'ipaddress' => $ipaddress,
'campaignid' => $campaignid,
'userid' => $userid,
'clickdate' => $datestmt['crdate']
));
$totalclicks = $stmt->fetch();
if($totalclicks['total'] >= 5){
$disable = $GLOBALS['link']->prepare('UPDATE tracking_redirect SET disabled=:disabled WHERE id=:clickid');
$disable->execute(array(
'disabled' => 1,
'clickid' => $clickid
));
if($totalclicks['total'] == 5){
$stmt = $GLOBALS['link']->prepare('SELECT * FROM user_login_history WHERE userid=:userid AND usertype=:usertype AND ipaddress=:ipaddress AND date(visitdate) BETWEEN :startdate AND :currentdate LIMIT 1');
$stmt->execute(array(
'userid' => $userid,
'usertype' => 1,
'ipaddress' => $ipaddress,
'startdate' => date('Y-m-d', strtotime('-3 months')),
'currentdate' => date('Y-m-d')
));
if($loginhistory = $stmt->fetch()){
}
else{
}
}
}
}
, While. . , while, , ..
, , . , . , ~ 10000 ~ 100 000 ?