Reputation: 15744
Here is my code:
$result_username = mysqli_query($dbconnection, Data::checkForUsername($username));
It then goes here:
public static function checkForUsername($username) {
global $database;
$query = "
SELECT COUNT(*)
FROM users
WHERE username='{$username}';";
$result = $database -> query($query);
return $result;
}
Then $result does this:
if (mysqli_result($result_username, 0) > 0) {
However, it then gives me back a Resource_Id?? I can not figure out why?
I simply want to check if the username exists in at least 1 row.
Upvotes: 2
Views: 671
Reputation: 92845
You need to fetch your data after you execute your query
$row = $result->fetch_array(MYSQLI_NUM);
return $row[0];
UPDATE: Now, using prepared statement your function can look like this:
public static function checkForUsername($username) {
global $database;
$result = 0;
$query = "SELECT COUNT(*) FROM users WHERE username=?";
/* create a prepared statement */
if ($stmt = $database->prepare($query)) {
/* bind parameters for markers */
$stmt->bind_param("s", $username);
/* execute query */
$stmt->execute();
/* bind result variable */
$stmt->bind_result($result);
/* fetch value */
$stmt->fetch();
/* close statement */
$stmt->close();
}
return $result;
}
Upvotes: 3
Reputation: 61
You can try
return $result->num_rows
Then
if (checkForUsername('Redeyes') != 0) {
}
Upvotes: 1