【问题标题】:mysql_fetch_array fails sometimesmysql_fetch_array 有时会失败
【发布时间】:2011-08-10 04:39:28
【问题描述】:

我正在尝试实现与在线支付框架的连接。

其中一个文件给我带来了一些麻烦,因为有时代码可以工作,有时却不行......而且我不明白为什么......

这是代码失败的地方......

$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 (!$hasil) {
  echo 'Stop1';
} else {
    if ($status=="Success") {}
}

这只是代码的一部分,但我认为这足以让您尝试查看问题......它在粗体线上失败,$checkout = mysql_fetch_array($result); 奇怪的是“echo sql”有效,它显示了正确的值,但是当我把它们放在数组上时,有时变量被传递,有时它们不是......所以,当到达@ 987654323@ 它失败了,因为值是空的......但有时它可以工作......

对可能发生的事情有什么想法吗?

比 路易斯

【问题讨论】:

  • 与其做if (!$hasil),不如试试if ($hasil === false)。它应该检查返回值是否为假,而不是该值是否等于假(因为假可能意味着 0、null 或 PHP 中的 '')
  • 你试过withecho吗"
    "; print_r($结帐);回声“
    ”;
  • *fin1te,我试过了,成功了!非常感谢 :) 一个简单而干净的解决方案 :d,Luis

标签: mysql arrays variables fetch


【解决方案1】:

这种失败的唯一方式是查询没有返回任何内容。

正确的方法是检查是否有返回:

$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";
}

【讨论】:

  • 感谢您的帮助 briedis,但现在我将使用 fin1te 解决方案
  • 但是,您仍然应该检查查询是否返回了某些内容,因为如果没有,您将收到通知错误并且不应忽略这些错误。
猜你喜欢
  • 2014-05-01
  • 2016-04-17
  • 2016-06-21
  • 2010-12-06
  • 2015-08-22
  • 1970-01-01
  • 1970-01-01
  • 2020-10-17
  • 1970-01-01
相关资源
最近更新 更多