I have the following code where I am validating receipts in an api and then I do an update in my base of what has changed in that receipt; what is happening to me is that at this moment in the base I have more than 1000 results but it is updating only one after it does the verification in the api, it updates one and throws a warning and does nothing else.
The mistake is. Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, string given in /var/www/custom-webservices/public/lemonchilli/cron/cron.php on line 23
require_once '../db_connect.php';
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$query = "SELECT * FROM jsonapple WHERE status='freetrial' AND expires_date_formatted_pst < (CURDATE() + INTERVAL 7 DAY) OR status='Active' AND expires_date_formatted_pst < (CURDATE() + INTERVAL 7 DAY) OR status='freetrial' AND auto_renew_status='false'";
$result = mysqli_query($conn, $query);
$sql = '';
if ($result) {
$count = 0;
while ($row = mysqli_fetch_assoc($result)) {
$count++;
$latest_receipt = $row["latest_receipt"];
$url = 'https://xxx/apple/verifyReceipt';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 2);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'token=xxx&receiptb64=' . $latest_receipt . '');
$result = curl_exec($ch);
//print $result;
curl_close($ch);
if (($count % 200) == 0) {
sleep(5);
}
$obj = json_decode($result, true);
if ($obj["auto_renew_status"] == 1) {
$auto_renew_status = 'true';
} else {
$auto_renew_status = 'false';
}
$status = $obj["status"];
if (isset($obj["expiration_intent"])) {
$expiration_intent = $obj["expiration_intent"];
} else {
$expiration_intent = '';
}
if (isset($obj["is_in_billing_retry_period"])) {
$is_in_billing_retry_period = $obj["is_in_billing_retry_period"];
} else {
$is_in_billing_retry_period = '';
}
if ($status == 0 || $status == 21006) {
if ($status == 21006) {
$status_row = 'Expired';
} else {
if ($obj["auto_renew_status"] == 1 && $obj["latest_receipt_info"]["is_trial_period"] == 'true') {
$status_row = 'Active';
}
elseif ($obj["auto_renew_status"] == 0 && $obj["latest_receipt_info"]["is_trial_period"] == 'true') {
$status_row = 'FreeTrial';
}
elseif ($obj["auto_renew_status"] == 1 && $obj["latest_receipt_info"]["is_trial_period"] == 'false') {
$status_row = 'Active';
$payment = 1;
}
elseif ($obj["auto_renew_status"] == 0 && $obj["latest_receipt_info"]["is_trial_period"] == 'false') {
$status_row = 'Expired';
}
}
} else {
$status_row = 'Review response';
}
$sql .= "UPDATE jsonapple SET status='$status_row', expiration_intent='$expiration_intent', auto_renew_status='$auto_renew_status', is_in_billing_retry_period='$is_in_billing_retry_period', status_code ='$status', payment='$payment' WHERE latest_receipt='$latest_receipt'; ";
}
if ($conn->query($sql) === TRUE) {
echo "Successfully" . $sql . "<br>";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
$conn->close();
Tell the junior that you are using $result = curl_exec($ch); that $result is the same variable you are using in mysqli_fetch_assoc($result) that blows it up.