Jump to content

mysqli_num_rows is giving me an error


chaseman

Recommended Posts

My code looks as follows:

 


include('connectvars.php');

/* REGISTER FORM */
// check if submit button has been clicked
if (isset($_POST['submit_signup'])) {

// process and assign variables after post submit button has been clicked
$user_email = strip_tags(trim($_POST['email']));
$firstname = strip_tags(trim($_POST['firstname']));
$lastname = strip_tags(trim($_POST['lastname']));
$nickname = strip_tags(trim($_POST['nickname']));
$password = $_POST['password'];
$repassword = $_POST['repassword'];
$dob = $_POST['dob'];
$find_us_question = strip_tags(trim($_POST['find_us_question']));

// connect to database
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

$check_query = "SELECT * FROM users WHERE nickname = '$nickname'";

$check_connect  = mysqli_query($dbc, $check_query);

$check_count =  mysqli_num_rows($check_connect);

echo $check_count;

die();
[/code]

 

 

It's a register (sign up) page, and it's the beginning of the script, the rest of the script is just checking if all fields are a empty and if the input is in the allowed character length etc. I could it off at die(); because the rest doesn't matter.

 

I want the script to check if the username already exists in the database, so I want mysqli_num_rows to tell me how many rows are already there with the same username, and then I want to continue doing an if statement saying

 

[code=php:0]
if ($check_count != 0) {
echo "Username already exists!"
}
[/code]

 

But the mysqli_num_rows doesn't even print out how many rows there are availible, it gives me an error saying:

 

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in...

 

 

The num_rows function worked in the login script the same way, but for some reason it's not working in the register script.

 

Any ideas, what I'm doing wrong?

 

For testing purposes I just want it to print me "1" when I'm entering a username that's already in the database.

 

 

Link to comment
Share on other sites

If you do a search on that error message, you will find that it means that your query failed due to an error of some kind.

 

For debugging purposes, use the following error checking on your query -

 

$check_connect  = mysqli_query($dbc, $check_query) or die(mysqli_error($dbc));

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.