mysql select查询结果为null所以如何在php中返回默认值

时间:2014-02-27 10:58:57

标签: php opencart

面对以下错误

  

错误:注意:第120行的/Applications/XAMPP/xamppfiles/htdocs/mks/opencart-1.5.6.1/upload/admin/controller/catalog/vendhqbridge.php中的未定义索引:id

相关代码:

//check for existing 'VendHQ_id' on  DB's product table

function checkVendHQid($flagChk)
{
$rs = $this->db->query("SELECT vendhq_id as id FROM ".DB_PREFIX."vendhq_product WHERE vendhq_id = '".$flagChk."'");

     $rowRsl=-1;
     if($rs===null)
    {
        $rowRsl = -1;
    }
    else
    {
       if($rs->row["id"] == $flagChk  )
       {
           $rowRsl = 1;
       }
    }

    return $rowRsl;
 }

2 个答案:

答案 0 :(得分:7)

FROM

之后加一个空格
$rs = $this->db->query("SELECT vendhq_id as id FROM ".DB_PREFIX."vendhq_product WHERE vendhq_id = '".$flagChk."'");

答案 1 :(得分:0)

如Vikas Umrao和shadyyx所述,您应该更改查询和if条件,如下所示:

更改

 $rs = $this->db->query("SELECT vendhq_id as id FROM ".DB_PREFIX."vendhq_product WHERE vendhq_id = '".$flagChk."'");

 $rowRsl=-1;
 if($rs===null)

 $rs = $this->db->query("SELECT vendhq_id as id FROM ".DB_PREFIX."vendhq_product WHERE vendhq_id = '".$this->db->escape($flagChk)."'");

 $rowRsl=-1;
 if(!$rs->num_rows){

度过美好的一天!!