Jump to content

Mysql_Num_Rows($Result) Not Working As Expected, Need Advise.


equivalents

Recommended Posts

Hey folks, I'm trying to create my first Login script using php to check for username and password in a MYSQL database. The script seems succesful at connecting to the database, however when I retrieve results from a table that I have confrimed exsist, the mysql_num_rows($result); returns zero, Can anyone suggest a solution or a diagnosis path?

 

Here is what I have:

 

LAMP server

PHP 5.4.4-7

MYSQL Ver 14.14 Distrib 5.5.24

 

 

<?php

ob_start();

$host="localhost"; // Host name

$username="pi"; // Mysql username

$password="raspberry"; // Mysql password

$db_name="n2it2_database"; // Database name

$tbl_name="email_list"; // Table name

 

// Connect to server and select databse.

$dbc = mysqli_connect("$host", "$username", "$password", "$db_name")or die("cannot connect");

 

 

 

$sql="SELECT * FROM email_list";

$result=mysqli_query($dbc, $sql) or die('Error quering database.');

 

// Mysql_num_row is counting table row

$count=mysql_num_rows($result);

 

// If result matched $myusername and $mypassword, table row must be 1 row

 

if($count==1){

 

// Register $myusername, $mypassword and redirect to file "login_success.php"

echo "Success!";

 

}

else {

echo "Wrong Username or Password";

}

 

ob_end_flush();

 

?>

login_script 2.php

Link to comment
Share on other sites

So, you are getting a result from mysql_num_rows() of 0 and no error is reported? That would mean the query is executing successfully, but that there were no matching records. In the above script I see where you connect to the DB server, but I don't see where you select what database to use.

 

But, you code makes no sense. You run a query to get ALL records from that table then check if there was exactly 1 result.IF there was one result then you echo a success message, else you have an error message for wrong username/password. So, if there were 0 records or more than 1 records in that table you would always get a failure. I think you meant for your query to find any records with the specific username and password.

Link to comment
Share on other sites

Hey Thanks for the reply!

 

So, you are getting a result from mysql_num_rows() of 0 and no error is reported? That would mean the query is executing successfully, but that there were no matching records. In the above script I see where you connect to the DB server, but I don't see where you select what database to use.

 

Doesn't this line do that?

$dbc = mysqli_connect("$host", "$username", "$password", "$db_name")or die("cannot connect");

 

But, you code makes no sense. You run a query to get ALL records from that table then check if there was exactly 1 result.IF there was one result then you echo a success message, else you have an error message for wrong username/password. So, if there were 0 records or more than 1 records in that table you would always get a failure. I think you meant for your query to find any records with the specific username and password.

 

I was just eliminating as many variables as possible, I also wanted to make the code short and easy to read for anyone helping me. I set the database up and it has only one table and the table only has one entry. So, i figured I would just start testing there and build the code up as I went along.

Link to comment
Share on other sites

YOu cannot mix mysqli (with an i) and mysql (no i) statements on the same connection.

 

You would need to use mysqli_num_rows (with an i).

 

Edit: and mysqli_connect() lets you specify the database as the 4th parameter.

Edited by PFMaBiSmAd
Link to comment
Share on other sites

Thankyou PFMaBiSmAd! Thanks for saving me quite a few hours, I have already spent many trying to solve that! I can move foward now! I wish I had know that beforehand! I just satrted, and I don't think the book I've been using mentions that, but I will check now.... My login form works now!

Edited by equivalents
Link to comment
Share on other sites

You need to set php's error_reporting to E_ALL and display_errors to ON to get php to help you find problems.

 

While the error message you would have been getting - Warning: mysql_num_rows() expects parameter 1 to be resource, object given in ... isn't explicitly telling you that you need to use the mysqli statement, it would have been calling your attention to that statement and throwing an error where normally there would not be an error.

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.