简体   繁体   English

PDO会知道mysql是否崩溃吗?

[英]Will PDO know if mysql crashed?

Forgive me if this is a silly question, but I'm in a difficult situation. 如果这是一个愚蠢的问题,请原谅我,但我处境艰难。

does 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);

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 that php will handle the case scenario bellow? 保证PHP将处理以下情况?

We have a payments system for a game, last time we put it out for testing, mysql settings for allocated memory weren't correct, so the db was refusing connections and didn't run the queries as it should, or at least some of them. 我们有一个用于游戏的支付系统,上次我们将其投入测试时,分配内存的mysql设置不正确,因此数据库拒绝连接并且未按需运行查询,或者至少其中一些他们。 So the result was, the paid flag in payments table not getting set and payments to be made over and over. 因此结果是,付款表中的已付款标志未设置,并且要一遍又一遍地付款。

Now this chunk of code up there is what follows the query and a SUCCESS message ( for communication ) my idea is if mysql crashes or fails in anyway, not to display the payments that should be made. 现在这部分代码后面是查询和一条SUCCESS消息(用于通信),我的想法是,无论如何mysql崩溃或失败,都不显示应付款。 Will the chunk above work in all cases? 上面的代码块在所有情况下都能工作吗?

Hope i made myself clear and ty 希望我能使自己清楚和坦率

edit: The code above was written mostly for demonstration, it's not correct code, as I noticed a bit late. 编辑:上面的代码主要是为了演示而编写的,它不是正确的代码,正如我注意到的有些晚。 But that shouldn't bother you because the original question is. 但这不应该打扰您,因为最初的问题是。 If PDO will handle db crashes and all the rest. 如果PDO将处理db崩溃以及所有其他崩溃。

my idea is for each Successfully updated row, to echo that particular payment, and that's not what the code above does. 我的想法是针对每个成功更新的行,以echo该特定的付款,这不是上面的代码所做的。 pretty messed up. 搞砸了

MORE EDITS 更多编辑

As you'll notice, I'm eager to get an answer about how safe I am. 您会注意到,我渴望得到关于我有多安全的答案。 (I hope it'll have states and facts for the reason why i'm safe or why not) So here it is, what I will probably keep as my code, unless told other wise. (我希望它能有状态和事实,以确保我为什么安全或为什么不安全)。因此,除非有其他说明,否则我将保留我的代码。 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]}";
}

It may not be the solution your looking for but you should always be checking your code as you go. 它可能不是您想要的解决方案,但是您应该始终在检查代码。 In this instance 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 fails to query) http://php.net/manual/en/pdo.query.php (如果查询失败,PDO将返回布尔值false)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM