php - How to effectively replace (mysql_result(mysql_query()) in PDO? -
as process complete rewriting web pdo instead of mysql_*
commands testing changed functions. , seems changed function mysql_result(mysql_query()
returns true, why that? lets see original , changed code:
if (mysql_result(mysql_query("select count(*) account id='".$_session["user_id"]."' , online=1"), 0)>0) { return true; } else return false;
and changed code here:
$stmt = $db_login->prepare("select count(*) account id=:id , online=1"); $stmt->bindvalue(':id', $_session["user_id"], pdo::param_int); $stmt->execute(); $results_login = $stmt->fetch(pdo::fetch_assoc); $rows = count($results_login); if ($rows > 0) { return true; } else return false;
so wrong why returns true when column has online=0? thank you
$stmt->fetch
fetches one row result set. out of array containing selected columns, looking this:
array( 'count(*)' => 42 )
a count()
on array result in 1
.
you need check contents of fetched row:
if ($result_login['count(*)'] > 0)
it's best alias column nicer name:
select count(*) `count` ...
then:
if ($result_login['count'] > 0)
Comments
Post a Comment