When I run this code, everything works:
<?php
$con = mysqli_connect ("", "", "", "") or die ($connect_error);
$username = 'tijmen';
$query = mysqli_query($con, "SELECT COUNT(*) FROM users WHERE username='$username'") or die (mysqli_error());
$result = mysqli_fetch_row($query);
echo $result[0];
?>
The username 'tijmen' exists in my database so the echo returns '1'.
Next thing I tried was:
$con = mysqli_connect ("", "", "", "") or die ($connect_error);
function user_exists($username) {
$query = mysqli_query($con, "SELECT COUNT(*) FROM users WHERE username='$username'") or die (mysqli_error());
$result = mysqli_fetch_row($query);
return ($result[0] == 1) ? true : false;
}
if (user_exists('tijmen') === true) {
echo 'user exists';
}
else {
echo 'not found';
}
When I run this code nothing happens and I don't know why. Can anyone help me out on this one?