Warning / Error

I am using Mysqli to discover is a user has activated their account and am trying to use this code.

function user_exists($username) {
$username = sanitize($username);
$query = "SELECT COUNT(user_id) FROM users WHERE username = $username";
return (mysqli_query($query, 0) == 1) ? true : false;

When I open the login page I get an error:

Warning: mysqli_query() expects parameter 1 to be mysqli, string given in C:\xampp\htdocs\cms\core\functions\users.php on line 5

Being new to mysql / mysqli any help to solve this matter will be appreciated.

Thanks in advanced

Check the docs, first parameter should be the db connection.

You should not add variables directly to your queries. Please read up on prepared/parameterized queries and use placeholders ? instead. Then you can skip that sanitize function as well

Thanks for the advice.

Sponsor our Newsletter | Privacy Policy | Terms of Service