I wrote PHP script to auto-update offers from provider API in my database. Unfortunately, by performing the following queries in the foreach loop (following the API records), the load is at 99% of the CPU. The script must be activated in cron every 10-15 minutes, but with this load the server is weak. How could I optimize it for more efficient work?
<?php
include "config.php";
function checkDevice($device) {
if($device == "Android" || $device == "iOS" || $device == "iPhone" || $device == "iPad") {
return true;
} else {
return false;
}
}
function countUserPayout($providerPayout) {
$summary = ($providerPayout * 100) / 2;
return ceil($summary);
}
function getOgAds() {
$response = file_get_contents("link");
$result = json_decode($response, true);
return $result["offers"];
}
function getAdGate() {
$response = file_get_contents("link");
$result = json_decode($response, true);
return $result["data"];
}
function checkOgAdsDevice($devices) {
$devicesArray = explode(",", $devices);
foreach($devicesArray as $device) {
if($device == "iPhone" || $device == "iPad" || $device == "Android") {
return true;
} else {
return false;
}
}
}
function detectDesktop($category) {
$categories = explode(",", $category);
$exists = array_search("Desktop", $categories);
if($exists) {
return true;
} else {
return false;
}
}
function createCountries($object) {
foreach($object as $country) {
$countries[] = $country;
}
if(isset($countries)) {
return $countries;
}
}
function createOfferWall() {
$offerWall = [];
foreach(getOgAds() as $offer) {
if(checkOgAdsDevice($offer["device"]) && detectDesktop($offer["device"]) == false) {
$offerWall[] = array(
"offer_id" => $offer["offerid"],
"name" => $offer["name_short"],
"requirements" => $offer["adcopy"],
"category" => $offer["device"],
"provider_payout" => $offer["payout"],
"payout" => countUserPayout($offer["payout"]),
"epc" => $offer["epc"],
"icon" => $offer["picture"],
"anchor" => $offer["link"],
"countries" => (array)$offer["country"],
"provider" => "ogads"
);
} else {
continue;
}
}
foreach(getAdGate() as $offer) {
if(checkDevice($offer["categories"][0])) {
$offerWall[] = array(
"offer_id" => $offer["id"],
"name" => $offer["adgate_rewards"]["anchor"],
"requirements" => $offer["requirements"],
"category" => $offer["categories"][0],
"provider_payout" => $offer["payout"],
"payout" => countUserPayout($offer["payout"]),
"epc" => $offer["epc"],
"icon" => $offer["creatives"]["icon"],
"anchor" => $offer["click_url"],
"countries" => createCountries($offer["countries"]),
"provider" => "adgate"
);
} else {
continue;
}
}
return $offerWall;
}
function createIdWall($dbh) {
$offerWall = createOfferWall();
foreach($offerWall as $offer) {
$idWall[$offer["offer_id"]] = $offer["offer_id"];
}
return $idWall;
}
function setCountries($dbh, $countries, $offer) {
if(isset($countries)) {
foreach($countries as $country) {
$stmt = $dbh->prepare("INSERT INTO `table`(`aaa`,`xxx`,`yyy`) VALUES(NULL, :country, :oid)");
$stmt->bindParam(":country", $country, PDO::PARAM_STR);
$stmt->bindParam(":oid", $offer, PDO::PARAM_INT);
$stmt->execute();
}
}
}
function checkOfferExists($dbh, $providerId) {
$stmt = $dbh->prepare("SELECT count(`id`) as `exists` FROM `table` WHERE `provider_id` = :providerId");
$stmt->bindParam(":providerId", $providerId, PDO::PARAM_STR);
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);
if($result["exists"] == 1) {
return true;
} else {
return false;
}
}
function getOfferInfo($dbh, $providerId) {
$stmt = $dbh->prepare("SELECT * FROM `table` WHERE `provider_id` = :providerId");
$stmt->bindParam(":providerId", $providerId, PDO::PARAM_STR);
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);
return $result;
}
function compareOffers($dbh, $providerId, $offer) {
$dbOffer = getOfferInfo($dbh, $providerId);
if($dbOffer["provider_payout"] != $offer["provider_payout"] || $dbOffer["anchor"] != $offer["anchor"] || $dbOffer["offer_name"] != $offer["name"] || $dbOffer["category"] != $offer["category"] || $dbOffer["icon"] != $offer["icon"] || $dbOffer["requirements"] != $offer["requirements"]) {
return true;
} else {
return false;
}
}
function getDbOffers($dbh) {
$stmt = $dbh->prepare("SELECT `provider_id` FROM `table`");
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $result;
}
function activateOffer($dbh, $providerId) {
$stmt = $dbh->prepare("UPDATE `table` SET `active` = 1 WHERE `provider_id` = :providerId");
$stmt->bindParam(":providerId", $providerId, PDO::PARAM_INT);
$stmt->execute();
}
function disableOffer($dbh, $providerId) {
$stmt = $dbh->prepare("UPDATE `table` SET `active` = 0 WHERE `provider_id` = :providerId");
$stmt->bindParam(":providerId", $providerId, PDO::PARAM_INT);
$stmt->execute();
}
function updateOffer($dbh, $providerId, $offer) {
$stmt = $dbh->prepare("UPDATE `table` SET
`aaa` = :payout,
`bbb = :points,
`ccc` = :anchor,
`ddd` = :name,
`eee` = :category,
`fff` = :icon,
`ggg` = :requirements
WHERE `provider_id` = :providerId");
$stmt->bindParam(":providerId", $providerId, PDO::PARAM_STR);
$stmt->bindParam(":payout", $offer["provider_payout"], PDO::PARAM_INT);
$stmt->bindParam(":points", $offer["payout"], PDO::PARAM_INT);
$stmt->bindParam(":anchor", $offer["anchor"], PDO::PARAM_STR);
$stmt->bindParam(":name", $offer["name"], PDO::PARAM_STR);
$stmt->bindParam(":category", $offer["category"], PDO::PARAM_STR);
$stmt->bindParam(":icon", $offer["icon"], PDO::PARAM_STR);
$stmt->bindParam(":requirements", $offer["requirements"], PDO::PARAM_STR);
$stmt->execute();
}
function addOffer($dbh, $offer) {
$stmt = $dbh->prepare("INSERT INTO `table` (`aaa`, `bbb`, `ccc, `ddd`, `eee`, `fff`, `ggg`, `hhh`, `jjj`, `kkk`, `lll`, `zzz`, `xxx`) VALUES (NULL, :requirements, :points, :providerId, :icon, :epc, :ownPayout, :anchor, :category, :name, 1, 0, :provider);");
$stmt->bindParam(":requirements", $offer["requirements"], PDO::PARAM_STR);
$stmt->bindParam(":points", $offer["payout"], PDO::PARAM_INT);
$stmt->bindParam(":providerId", $offer["offer_id"], PDO::PARAM_INT);
$stmt->bindParam(":icon", $offer["icon"], PDO::PARAM_STR);
$stmt->bindParam(":epc", $offer["epc"], PDO::PARAM_STR);
$stmt->bindParam(":ownPayout", $offer["provider_payout"], PDO::PARAM_STR);
$stmt->bindParam(":anchor", $offer["anchor"], PDO::PARAM_STR);
$stmt->bindParam(":category", $offer["category"], PDO::PARAM_STR);
$stmt->bindParam(":name", $offer["name"], PDO::PARAM_STR);
$stmt->bindParam(":provider", $offer["provider"], PDO::PARAM_STR);
$stmt->execute();
setCountries($dbh, $offer["countries"], $offer["offer_id"]);
}
function checkStatus($dbh, $providerId) {
$stmt = $dbh->prepare("SELECT `active` FROM `table` WHERE `provider_id` = :providerId");
$stmt->bindParam(":providerId", $providerId, PDO::PARAM_INT);
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);
if($result["active"] == 0) {
return false;
} else {
return true;
}
}
function followOffers($dbh) {
foreach(createOfferWall() as $offer) {
if(checkOfferExists($dbh, $offer["offer_id"])) {
if(compareOffers($dbh, $offer["offer_id"], $offer)) {
updateOffer($dbh, $offer["offer_id"], $offer);
} else {
continue;
}
} else {
addOffer($dbh, $offer);
}
}
}
function findRevokeOffers($dbh) {
$idWall = array_flip(createIdWall($dbh));
foreach(getDbOffers($dbh) as $dbOffer) {
$providerId = $dbOffer["provider_id"];
if(isset($idWall[$providerId])) {
continue;
} else {
if(checkStatus($dbh, $providerId)) {
disableOffer($dbh, $providerId);
} else {
continue;
}
}
}
}
function getCurrentTime() {
$currentTime = date('Y-m-d H:i:s');
return $currentTime;
}
echo "// START [".getCurrentTime()."]" .PHP_EOL;
followOffers($dbh);
findRevokeOffers($dbh);
echo "// END" .PHP_EOL;
?>
I have a page that upload files to my database, im trying to record the day when the file is uploaded so i can check and view it to my table, but everytime i upload a file, it doesnt record any dates
<?php
function upload_image()
{
if ( isset($_FILES["user_image"]) )
{
$date = date('M-D-Y');
$destination = './upload/' . $_FILES['user_image']['name'];
move_uploaded_file($_FILES['user_image']['tmp_name'], $destination);
return $_FILES['user_image']['name'];
}
}
function get_image_name($user_id)
{
include('db3.php');
$statement = $connection->prepare("SELECT image FROM users1 WHERE id = '$user_id'");
$statement->execute();
$result = $statement->fetchAll();
foreach($result as $row)
{
return $row["image"];
}
}
function get_total_all_records()
{
include('db3.php');
$statement = $connection->prepare("SELECT * FROM users1");
$statement->execute();
$result = $statement->fetchAll();
return $statement->rowCount();
}
?>
My Insert Query:
<?php
include('db3.php');
include('function.php');
if(isset($_POST["operation"]))
{
if($_POST["operation"] == "Add")
{
$image = '';
if($_FILES["user_image"]["name"] != '')
{
$image = upload_image();
}
$statement = $connection->prepare("
INSERT INTO users1 (memorandum, titlee, image, date)
VALUES (:memorandum, :titlee, :image, date)
");
$result = $statement->execute(
array(
':memorandum' => $_POST["memorandum"],
':date' => $_POST["date"],
':titlee' => $_POST["titlee"],
':image' => $image
)
);
if(!empty($result))
{
echo 'Data Inserted';
}
}
if($_POST["operation"] == "Edit")
{
$image = '';
if($_FILES["user_image"]["name"] != '')
{
$image = upload_image();
}
else
{
$image = $_POST["hidden_user_image"];
}
$statement = $connection->prepare(
"UPDATE users1
SET memorandum = :memorandum, titlee = :titlee, image = :image , date = :date ,
WHERE id = :id
"
);
$result = $statement->execute(
array(
':memorandum' => $_POST["memorandum"],
':titlee' => $_POST["titlee"],
':date' => $_POST["date"],
':image' => $image,
':id' => $_POST["user_id"]
)
);
if(!empty($result))
{
echo 'Data Updated';
}
}
}
?>
I have two different databases with table Phpfox_user , manual_userid and mobile table in them.There can be 3 users with same user_id but different mobile numbers stored in phpfox_user table and manual_userid and there GCM id in table mobile.(with mobile no column as well). Now i want to send notification to all mobile numbers with same user id .
Currently notification is only sent to a single manager only
function request_block($data){
//print_r($data);
$tower=new Tower;
$project=new Project;
$note=new Notification;
$fields=array('user_id', 'pay_opt', 'sale_type', 'hold_status', 'cost', 'is_manager_blocked', 'is_action_taken');
$params=array(
':user_id' => $data['user_id'],
':pay_opt' => $data['buy_opt'],
':sale_type' => $data['sale_type'],
':hold_status' => '1',
':cost' => $data['cost'],
':is_manager_blocked' => '0',
':is_action_taken' => '0',
':id' => $data['flat_id']
);
$where = 'where id= :id';
$tower->block_flat($fields, $params, $where);
$user_details=$tower->get_user_details($data['user_id']);
$flat_details=$tower->get_flat_details_cost($data['flat_id']);
$tower_main=$tower->get_tower_main_details($flat_details['tower_id']);
$project_details=$project->get_project_details($tower_main['project_id']);
$manager_details=$tower->get_user_details($project_details['manager_id']);
$manager_details=$tower->get_manualuser_details($project_details['manager_id']) ;
$mob_details=$tower->get_gcmid($manager_details['cf_mobile']);
$par['mobile']=$manager_details['cf_mobile'] ;
//print_r($flat_details); print_r($tower_main); print_r($project_details);
//$manager_details=$tower->get_user_details($project_details['manager_']);
$data['send_by']='user';
$data['user_id']=$data['user_id'];
$data['manager_id']=$project_details['manager_id'];
$data['message']='You have received a Booking request - by '.$user_details['full_name'].' '.$user_details['cf_mobile'].' for Flat no. '.$flat_details['flat_no'].' in '.$tower_main['tower_name'].' tower of '.$project_details['project_name'].' Project';
$data['msg_details']=$data['message'];
$data['notification_type']='user-to-manager';
$note->insert_notification($data);
$par['msg']=$data['message'];
if(count($mob_details)>=1 && $mob_details['gcmid']!='')
{
$fuck['notification_type']='user-to-manager';
$fuck['notification_text']=$par['msg'];
$fuck['user_id']=$data['user_id'];
$tt['message']=$fuck;
$tt['gcmid']=$mob_details['gcmid'];
send_notification($tt);
}
if(count($tower_images)>=1)
{
$result['data']=$tower_images;
} else {
$result['message']='No result found';
}
$result['message']='Flat block requested';
//print_array($par);
$result['status']='success';
send_sms($par);
$res=json_encode(array($result));
echo $res;}
These are the two functions that gets the user mobile numbers
public function get_user_details($user_id)
{
$params = array( ':user_id' => $user_id);
$sql = "SELECT * FROM phpfox_user, phpfox_user_custom where phpfox_user.user_id=phpfox_user_custom.user_id AND phpfox_user.user_id=:user_id";
$stmt = parent::query($sql, $params, '', 'main');
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) :
return $result[]=$row;
endwhile;
return $result;
}
public function get_manualuser_details($user_id)
{
$params = array( ':user_id' => $user_id);
$sql = "SELECT * FROM manual_userid where user_id=:user_id";
$stmt = parent::query($sql, $params);
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) :
return $res=$row;
endwhile;
return $result;}
public function get_gcmid($data)
{
$mobile=str_replace('+','0',$data);
$params = array( ':mobile' => $mobile);
$sql = "SELECT * FROM mobile where mobile=:mobile";
$stmt = parent::query($sql, $params);
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) :
return $res=$row;
endwhile;
}
I need to store the rows in an array but can't seem to get it to work, the code is below:
I am sure $result = $stmt->get_result(); does not work.
When i test in the browser output is empty.
<?php
include "connect.php";
$uid = $_REQUEST["U_ID"];
$stmt = $mysqli->prepare("SELECT * FROM U_User WHERE U_ID = ?");
$stmt->bind_param("d", $uid);
$stmt->execute();
$stmt->fetch();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc())
{
$users[] = $row;
}
$stmt->close();
$arr = array('user' => $users);
echo json_encode($arr);
mysqli_close($conn);
?>
Just like php_nub_qq said. I switched to PDO works great.
<?php
include "connect.php";
$uid = $_REQUEST["U_ID"];
$q = $conn->prepare("SELECT * FROM U_User WHERE U_ID = :uid");
$q->execute(array(':uid' => $uid));
$q->setFetchMode(PDO::FETCH_ASSOC);
while ($r = $q->fetch()) {
$users[] = $r;
}
$q = $conn->prepare("SELECT * FROM P_Post u WHERE P_U_ID = :uid");
$q->execute(array(':uid' => $uid));
$q->setFetchMode(PDO::FETCH_ASSOC);
while ($r = $q->fetch()) {
$posts[] = $r;
}
$q = $conn->prepare("SELECT * FROM T_Thread u WHERE T_U_ID = :uid");
$q->execute(array(':uid' => $uid));
$q->setFetchMode(PDO::FETCH_ASSOC);
while ($r = $q->fetch()) {
$threads[] = $r;
}
$arr = array('user' => $users, 'post' => $posts, 'thread' => $threads);
echo json_encode($arr);
$conn = null;
?>
I've this function:
private function db_bind_array($stmt, &$row) {
$md = $stmt->result_metadata();
$param = array();
while($field = $md->fetch_field()) { $param[] = &$row[$field->name];}
return call_user_func_array(array($stmt, 'bind_result'), $param);
}
private function db_query($sql, $bind_param, $param) {
if($stmt = $this->conn->prepare($sql)) {
if(!$bindRet = call_user_func_array(array($stmt,'bind_param'),
array_merge(array($bind_param), $param))) $this->Terminate();
if(!$stmt->execute()) $this->Terminate();
$res = array();
if($this->db_bind_array($stmt, $res)) return array($stmt, $res);
}
}
protected function Select($recs, $table, $where, $bind_param, $param, $order_by = '', $sort = '', $limit = 1) {
if($order_by != '') $order_by = 'ORDER BY '.$order_by;
$sql = "SELECT $recs FROM $table WHERE $where $order_by $sort LIMIT $limit";
return $this->ExeSelect($sql, $bind_param, $param);
}
private function ExeSelect($sql, $bind_param, $param) {
if($res = $this->db_query($sql, $bind_param, array(&$param))) {
$stmt = $res[0]; $row = $res[1];
while($stmt->fetch()) {$this->row = $row; return $row;}
$stmt->close();
}
}
And I use it:
$row = $this->Select('id, name, title, 'Articles', where id >, 'i', 10, 'DESC', '', 10)
The problem is that it returns only one record instead of 10.
What's the problem?
Thanks
The problem is this line: while($stmt->fetch()) {$this->row = $row; return $row;}. You immediately return that result. Build an array before you return it.
private function ExeSelect($sql, $bind_param, $param) {
$ret = array();
if($res = $this->db_query($sql, $bind_param, array(&$param))) {
$stmt = $res[0]; $row = $res[1];
while($stmt->fetch()) {$ret[] = $row; }
$stmt->close();
}
return $ret;
}