Kirito Kirito - 1 month ago 15
MySQL Question

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\medcares\index.php on line 12

I'am creating a register.php and I'am now in the phase of creating "Username and Email Checking in Database" and somehow I always get an error. Please help me fix this. Thanks

<?php
require_once('pages/connect.php');
if (isset($_POST['username']) && isset($_POST['password'])){
$username = mysqli_real_escape_string($connection, $_POST['username']);
$email = mysqli_real_escape_string($connection, $_POST['email']);
$password = md5($_POST['password']);
$repassword = md5($_POST['repassword']);
if ($password == $repassword) {
$fmsg = "";
$usernamesql = "SELECT * FROM 'users' WHERE username='$username'";
$usernameres = mysqli_query($connection,$usernamesql);
$count = mysqli_num_rows($usernameres);
if($count == 1){
$fmsg .= "Username Already Exist!";
}

$query = "INSERT INTO 'users' (username, password, email) VALUES ('$username', '$password', '$email')";
$result = mysqli_query($connection, $query);
if($result){
$smsg = "User Created Successfully.";
}else{
$fmsg .= "Failed to Register User";
}
} else {
$fmsg = "Password Do not Match";
}
}
?>


I already check many times the

$usernamesql = "SELECT * FROM 'users' WHERE username='$username'";


but I still get error messages. Please help me. Thank You for your help.

Answer

This issue is invariably caused by a failed SQL statement, which results in a false return value. This should be checked to ensure you don't then try to use it as, for example, a rowset.

In this particular instance, you should be using users (or the equivalent with backticks, though it's not necessary in this case) rather than 'users'. The former is a database object, the latter is a string literal.

In other words, one of:

$usernamesql = "SELECT * FROM  users  WHERE username='$username'";
$usernamesql = "SELECT * FROM `users` WHERE username='$username'";