Will PDO know if mysql crashed?

Forgive me if this is a stupid question, but I'm in a difficult situation.

does the following:

$query = "SELECT `av`.`UUID`, `pay`.`amount`, `pay`.`id` FROM `2starsglobal`.`avatars` AS av RIGHT JOIN `payments` AS `pay` ON `av`.`id` = `pay`.`avatarId` WHERE `pay`.`payed` = 0 AND `pay`.`verificationPending` = 0 ORDER BY `pay`.`id` ASC LIMIT 0, 14"; $stmt = $db->query($query); echo 'SUCCESS'; $payments = ""; while($row = $stmt->fetch(PDO::FETCH_NUM)) { $payments .= ",{$row[0]},{$row[1]},{$row[2]}"; //echo ',', $row[0],',', $row[1],',', $row[2]; $query = "UPDATE `payments` SET `verificationPending`=1 WHERE `id`={$row[2]}"; } try { $db->query($query)->closeCursor(); } catch(PDOException $e) { return; } echo $payments; 

guarantee php will handle the script below?

We have a payment system for the game, the last time we put it out for testing, the mysql settings for the allocated memory were incorrect, so db refused connections and did not start queries as it should, or at least some of them. Thus, the result was: a paid flag was not set in the payment table, and payments should be made again and again.

Now this piece of code contains what follows the request and the message SUCCESS (for communication), my idea is that if mysql crashes or crashes in any case, do not display the payments that must be made, will the inscription above work in all cases?

Hope I made it clear and

edit The above code was written mainly for demonstration, this is not the correct code, as I noticed a bit late. But this should not bother you, because the original question. If PDO will handle db failure and everything else.

My idea is for each successfully updated line, echo for this particular payment, and this is not what the code above does. quite messed up.

MORE EDITING

As you noticed, I really want to get an answer about how safe I am. (I hope he will have states and facts for the reason why I am safe and why not). So that’s what I’ll probably be storing as my code, if not to say wise to others. Should I feel safe with this?

 $query = "SELECT `av`.`UUID`, `pay`.`amount`, `pay`.`id` FROM `2starsglobal`.`avatars` AS av RIGHT JOIN `payments` AS `pay` ON `av`.`id` = `pay`.`avatarId` WHERE `pay`.`payed` = 0 AND `pay`.`verificationPending` = 0 ORDER BY `pay`.`id` ASC LIMIT 0, 14"; $stmt = $db->query($query); $payments = $stmt->fetchAll(); $query = "UPDATE `payments` SET `verificationPending`=1 WHERE "; foreach($payments as $payment) { $query .= "`id`={$payment[2]} AND "; } $query = substr($query, 0, strlen($query-5)); try { $db->beginTransaction(); $db->query($query)->closeCursor(); $db->commit(); } catch(PDOException $e) { $db->rollBack(); return; } echo "SUCCESS"; foreach($payments as $payment) { echo ",{$payment[0]},{$payment[1]},{$payment[2]}"; } 
+4
source share
1 answer

This may not be the solution you are looking for, but you should always check your code when you go. In this case, you want to return success if the record exists ...

 $query = '...'; $stmt = $db->query($query); if($stmt === false) // Query failed to execute while($row = $db->fetch(PDO::FETCH_NUM)) { ... } $stmt = $db->query($query); if($stmt === false) // Update failed $stmt->closeCursor(); // NOW you can echo 

http://php.net/manual/en/pdo.query.php (PDO returns boolean false if it cannot fulfill the request)

+2
source

All Articles