Harun Harun - 3 months ago 11
SQL Question

Difficulty converting mysql_result

I was following up a rather older tutorial on youtube and in the video he used mysql_result in this way

return (mysql_result($result,0) == 1) ? true : false;


And I figured mysql_result is deprecated and wanted to do the exact same thing using num rows and did it like this:

$query = "SELECT COUNT(`user_id`) FROM `users` WHERE `username`='$username'";
$result = mysqli_query($conn,$query);
return(mysqli_num_rows($result) === 1) ? true : false;


But doesn't seem to be working and can not figure out where i went wrong......and suggestions??

Answer

Try something like this

$query = "SELECT COUNT(`user_id`) as user_count FROM `users` WHERE `username`='$username'";
$result = mysqli_query($conn,$query);
$row = mysqli_fetch_array($result,MYSQLI_ASSOC);
return(($row["user_count"]) === 1) ? true : false;
Comments