简体   繁体   中英

mysql select query result is null so how can i return default value in php

Facing the following error

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

Relevant code:

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

FROM之后放置一个空格

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

As mentioned by Vikas Umrao and shadyyx, you should change the query and the if condition as below:

Change:

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

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

To:

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

Have a nice day !!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM