开发者

mysql_fetch_array fails sometimes

开发者 https://www.devze.com 2023-02-28 01:01 出处:网络
I\'m trying to implement the connection to a online payment framework. One of the files is giving me some trouble, because sometimes the code works, sometimes it doesn\'t... And I can\'t understand w

I'm trying to implement the connection to a online payment framework.

One of the files is giving me some trouble, because sometimes the code works, sometimes it doesn't... And I can't understand why...

Here's where the code is failing...

$sql = "select transidmerchant,totalamount from nsiapay where     transidmerchant='".$order_number."'and trxstatus='Verified'";
$result = mysql_query($sql);
**$checkout = mysql_fetch_array($result);**
echo "sql : ".$sql;
$hasil=$checkout['transidmerchant'];
echo "hasil: ".$hasil;
$amount=$checkout['totalamount'];
echo "amount: ".$amount;
    // Custom Field
if开发者_StackOverflow社区 (!$hasil) {
  echo 'Stop1';
} else {
    if ($status=="Success") {}
}

It's just part of the code but I think it's enough for you to try to see the problem... It fails on the bold line, $checkout = mysql_fetch_array($result); The weird thing is that the "echo sql" works, and it shows the right values, but then when I put them on the array, sometimes the variables are passed, sometimes they're not... And so, when getting to if (!$hasil) it fails because the value is empty... but sometimes it works...

Any ideas on what might be happen?

Thans Luis


The only way this fail is when query doesn't return anything.

The correct way would be to check if there is something returned:

$sql = "select transidmerchant,totalamount from nsiapay where     transidmerchant='".$order_number."'and trxstatus='Verified'";
$result = mysql_query($sql);
if($checkout = mysql_fetch_array($result)){
    $hasil = $checkout['transidmerchant'];
    echo "hasil: ".$hasil;
    $amount=$checkout['totalamount'];
    echo "amount: ".$amount;
        // Custom Field
    if (!$hasil) {
      echo 'Stop1';
    } else {
        if ($status=="Success") {}
    }
}else{
    echo "Empty query result";
}
0

精彩评论

暂无评论...
验证码 换一张
取 消