-2
$con = mysqli_connect("localhost","root","","database");

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

I believe there is an error in the query because I always get "Username is taken" error, doesnt matter if I insert an existing username or not.

** SOLVED THANKS **

2
  • Can you copy the current error? Commented Nov 22, 2013 at 11:26
  • You haven't got $con defined in the function scope. You either need to pass it in with your function parameters, set up the connection within the function itself, or pass it in via a globally accessible class (such as $con = dbmanager::getConnection()). Commented Nov 22, 2013 at 11:38

1 Answer 1

0

try this

$query = "SELECT COUNT(user_id) as user_id FROM users WHERE `username` = '$username'";
$result = mysqli_query($con, $query);

mysql_result will not return you the result , it will send error so it gives you true always.

use below code to get result

$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
if($row['user_id']){ return true; } else { return false; }
Sign up to request clarification or add additional context in comments.

1 Comment

do not use mysql_result use mysqli_fetch_array check updated answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.