Nevondrax Nevondrax - 1 year ago 97
SQL Question

MySQL & PHP, mysqli_num_rows always returns 0

I have a problem that i can't seem to be able to solve with my register script.

Here's the script:


//MySQLi connection

$con = mysqli_connect("-","-","-","users");

if (mysqli_connect_errno())


echo "MySQLi Connection was not established: " . mysqli_connect_error();


//Reading the userdata from the registerp.php page

$usr = mysqli_real_escape_string($con,$_POST['username']);

$email = mysqli_real_escape_string($con,$_POST['email']);

$pass_unhashed = mysqli_real_escape_string($con,$_POST['pass']);

$pass = password_hash($pass_unhashed, PASSWORD_DEFAULT);

//Checking if user exists

$check_usr = mysqli_query($con,"SELECT * FROM users WHERE user_name = '$usr'");
if(check_usr === false)
if (mysqli_num_rows($con,$check_usr)>=1)
echo "This Username already exists";
echo "This Username is available";
echo " Name: $usr";

My problem is that i can't get the verification to work (So that they can't create two accounts with the same names).

always returns 0 ( "This Username is available" ) even though there is my test user (nevondrax) in my table

Also, it doesn't seem to give me any errors either..

What did i do wrong / What can i do to fix it?

Answer Source

mysqli_num_rows() accepts single argument of result set (mysqli_query() or mysqli_store_result() or mysqli_use_result()), So it should be,



Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download