2014-02-27 78 views
1

返回默認值面對以下錯誤MySQL的選擇查詢結果爲空我怎麼能在PHP

Error: Notice: Undefined index: id in /Applications/XAMPP/xamppfiles/htdocs/mks/opencart-1.5.6.1/upload/admin/controller/catalog/vendhqbridge.php on line 120

相關代碼:

//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; 
} 

回答

1

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){ 

一個愉快的一天有!

7

在空格後FROM

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

此外,如果'$ flagChk'將會被轉義:'$ this-> db-> escape($ flagChk)''。 – shadyyx