I would like to test whether an entry of a particular UID and MID exists in the table user_saved, so here's what I did
$result = $db->prepare("SELECT * FROM user_saved WHERE UID = ? AND MID = ?");
$result->execute(array($_SESSION['uid'], $itemId['M开发者_如何学GoID2']));
$test = $result->fetch(PDO::FETCH_ASSOC);
if (isset($test)){
exist
} else {
doesn't exist
}
The issue is that I tried with a UID and MID that doesn't exist, but it always goes into the isset if statement, why is this? What's a better way to do this?
isset checking whether variable exists or not use strlen function or array count function
<?php
$bbnq = sprintf("SELECT login
FROM users
WHERE id = %u",27);
try
{ $req = $db_bbn->query($bbnq); }
catch (Exception $e)
{ bbnf_pdo_error($e,__FILE__,__LINE__); }
if ( $r = $req->fetch() )
{ echo "This query has a row result"; }
else
{ echo "This query has an empty result"; }
?>
fetch
returns FALSE
on failure:
if ($test !== FALSE) // ...
精彩评论