I'm getting this error, why does it expect the parameter to not be a boolean? How do I fix this? (I know this is a common error, so I want to understand WHY it comes up, so that I can hopefully fix it on my own when it comes up again.)
Warning: mysqli_stmt_num_rows() expects parameter 1 to be mysqli_stmt, boolean given in C:\Users\James\Desktop\Container\XAMPP\htdocs\Triiline1\signupuser.php on line 20
<!doctype html>
<html>
<head>
<title>User Signup</title>
</head>
<body>
<?php
include 'connect.php';
//if submit is clicked
if (isset($_POST['submit'])) {
//then check if all fields are filled
if (empty($_POST['username']) | empty($_POST['password']) | empty($_POST['firstname']) | empty($_POST['MI']) | empty($_POST['lastname']) | empty($_POST['email']) | empty($_POST['phonenumber']) | empty($_POST['country']) ) {
echo('You did not complete all of the required fields'); }
$username = $_POST['username'];
$password = $_POST['password'];
$usernamesquery = mysql_query("SELECT * FROM logins WHERE username='$username'");
if(mysqli_stmt_num_rows($usernamesquery) > 0) {
die('This username is already taken.');
}
} ?>
<form action="" method="post">
Username: <input type="text" name="username" maxlength="30"><br>
Password: <input type="password" name="password" maxlength="30"><br>
First Name: <input type="text" name="firstname" maxlength="30"><br>
Middle Initial: <input type="password" name="MI" maxlength="30"><br>
Last Name: <input type="text" name="lastname" maxlength="30"><br>
Email: <input type="password" name="email" maxlength="50"><br>
Phone Number: <input type="text" name="phonenumber" maxlength="11"><br>
Country: <input type="password" name="country" maxlength="40"><br>
<input type="submit" name="submit">
</form>
</body>
</html>
You are using mysql_*
in this statement instead of mysqli_*
$usernamesquery = mysql_query("SELECT * FROM logins WHERE username='$username'");
Try:
$usernamesquery = mysqli_query($connection, "SELECT * FROM logins WHERE username='$username'");
EDIT
You need to use mysqli_num_rows($usernamesquery)
instead of mysqli_stmt_num_row(...)
in your current implementation:
mysqli_stmt_num_rows()
is used as:
if ($stmt = mysqli_prepare($connection, $query)) {
/* execute query */
mysqli_stmt_execute($stmt);
/* store result */
mysqli_stmt_store_result($stmt);
printf("Number of rows: %d.
", mysqli_stmt_num_rows($stmt));
}
Try to use either mysqli
or mysql
dont mix up..you are using both
$usernamesquery = mysqli_query("SELECT * FROM logins WHERE username='$username'");
if(mysqli_stmt_num_rows($usernamesquery) > 0) {
die('This username is already taken.');
}