Jump to content

Retrieving ID from table with Db::getInstance


jonsecu

Recommended Posts

Hi... I want a function that looks for an entry in a table, if the entry exists it should return the ID, if not return false. The function is getRowFromElement... it is a SELECT WHERE Query, I just pass the variables to Db::getInstance. To validate if there is such record I ask if count($results)>0 (there are unique entries for this field on the table). If count($results)>0 I try to return $results[0] as the ID of the element I am looking for. However, instead of returning an int, it returns an array. 

 

Here is the code... someone could please help me out?

 

Thank you.

$table = 'pfm_dir';
 $field = 'id_pfm_dir';
 $where = 'email';
 $condition = $inputEmail;
 $results = getRowFromElement($field,$table,$where,$condition);
 if (count($results)>0) {
    saveMsg($results[0] , $inputMessage);
    saveToLog('people.log',$inputName . $inputEmail . $inputMessage . $inputType . $productID . var_dump($results) . " EXISTE\n");
   }

function getRowFromElement($field,$table,$where,$condition) {
      $sql = "SELECT " . $field . " FROM "._DB_PREFIX_. $table ." WHERE " . $where . " = " . "'" . $condition . "'";
      return Db::getInstance()->ExecuteS($sql);
    }
Link to comment
Share on other sites

Thank you very much... it worked well. I finally used:

 

return Db::getInstance()->getValue($sql);

 

Hi

 

Instead of

return Db::getInstance()->ExecuteS($sql);

Use

return Db::getInstance()->getRow($sql);

Regards

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...