如何获取我的php文件的这一部分(查询我的dbase),运行5次然后停止,如果条件在第5次尝试后保持为假?
以下是我要循环的文件部分:
if ((isset($_POST['_Commandeviapaypal'])) && ($PaypalStatus != "Completed"))
{
sleep(60);
$PaypalPayment = "SELECT payment_amt FROM al_paypal WHERE ((item_number='" .$No . "') || (item_number='" .$NoGift . "')) ";
$query1 = mysql_query($PaypalPayment);
$row1 = mysql_fetch_row($query1);
$PaypalPayment = $row1[0];
$PaypalConfirmation = "SELECT txn_id FROM al_paypal WHERE ((item_number='" .$No . "') || (item_number='" .$NoGift . "')) ";
$query2 = mysql_query($PaypalConfirmation);
$row2 = mysql_fetch_row($query2);
$PaypalConfirmation = $row2[0];
$PaypalStatus = "SELECT payment_status FROM al_paypal WHERE ((item_number='" .$No . "') || (item_number='" .$NoGift . "')) ";
$query3 = mysql_query($PaypalStatus);
$row3 = mysql_fetch_row($query3);
$PaypalStatus = $row3[0];
}
感谢您的帮助。
答案 0 :(得分:0)
添加一个变量,记录您到目前为止的尝试次数,并在达到5时停止循环。
if ((isset($_POST['_Commandeviapaypal'])) && ($PaypalStatus != "Completed"))
{
$attempts = 0;
while ($attempts < 5)
{
// other code
if (/* successful */)
{
break;
}
$attempts = $attemps + 1;
}
}
答案 1 :(得分:0)
你可以使用for循环迭代5次,循环中有一个额外的条件,如下所示:
for ($i = 0; $i < 5 && (CONDITION); ++$i) {
//Code here
}