equivalents Posted December 3, 2012 Share Posted December 3, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/271552-mysql_num_rowsresult-not-working-as-expected-need-advise/ Share on other sites More sharing options...
Psycho Posted December 3, 2012 Share Posted December 3, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/271552-mysql_num_rowsresult-not-working-as-expected-need-advise/#findComment-1397285 Share on other sites More sharing options...
equivalents Posted December 3, 2012 Author Share Posted December 3, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/271552-mysql_num_rowsresult-not-working-as-expected-need-advise/#findComment-1397288 Share on other sites More sharing options...
MDCode Posted December 3, 2012 Share Posted December 3, 2012 (edited) You can not use mysql commands with mysqli. It will not work. Choose one. Edited December 3, 2012 by SocialCloud Quote Link to comment https://forums.phpfreaks.com/topic/271552-mysql_num_rowsresult-not-working-as-expected-need-advise/#findComment-1397294 Share on other sites More sharing options...
PFMaBiSmAd Posted December 3, 2012 Share Posted December 3, 2012 (edited) 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 December 3, 2012 by PFMaBiSmAd Quote Link to comment https://forums.phpfreaks.com/topic/271552-mysql_num_rowsresult-not-working-as-expected-need-advise/#findComment-1397295 Share on other sites More sharing options...
equivalents Posted December 3, 2012 Author Share Posted December 3, 2012 (edited) 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 December 3, 2012 by equivalents Quote Link to comment https://forums.phpfreaks.com/topic/271552-mysql_num_rowsresult-not-working-as-expected-need-advise/#findComment-1397298 Share on other sites More sharing options...
PFMaBiSmAd Posted December 3, 2012 Share Posted December 3, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/271552-mysql_num_rowsresult-not-working-as-expected-need-advise/#findComment-1397300 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.