Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

function to check if id exist - does not work? #46

Closed
futurewebsites opened this issue Jun 10, 2015 · 1 comment
Closed

function to check if id exist - does not work? #46

futurewebsites opened this issue Jun 10, 2015 · 1 comment

Comments

@futurewebsites
Copy link

Hi I have written a function to see if an id in a given table exists but it doesn't work any ideas why?

 function doesExist($table,$where,$whereArray,$db)
 {
     $chk = $db->query("SELECT id FROM $table WHERE $where",$whereArray);
    if(count($chk)>0)
     {
         echo $chk['id']; exit();
         return $chk['id'];
     }
    else
    {
    return false;
     }
 }

It always returns false :( I pass the values to it like:
$table = "tablename";
$where = "field = :field";
$chkArray = ["field"=>"value"];
$chk = doesExist($table,$where,$chkArray,$db);

@YamilBracho
Copy link

I use this code :
public function exists($table, $where = NULL) {
$sql = "SELECT EXISTS(SELECT 1 FROM $table ";

    if (!is_null($where)) {
        $sql .= " WHERE $where ";
    }

    $sql .= ' LIMIT 1)';
    $row = $this->db->column($sql, null);
    return $row[0];
}

And to call

$found = exists('tablename', 'field = value');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants