haris244808 Posted August 6, 2011 Share Posted August 6, 2011 HI im getting this error when it counts my number of rows.how to fix it?: Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\MyOnlineStore\storeadmin\admin_login.php on line.... Here is a part of admin_login code : <?php //Parse the log in form if the user has filled out and pressed "Log In" if(isset($_POST["username"])&&($_POST["password"])){ $manager=preg_replace('#[A-Za-z0-9]#i','',$_POST["username"]);//Filter everything but numbers and letters $password=preg_replace('#[A-Za-z0-9]#i','',$_POST["password"]);//Filter everything but numbers and letters //Connect to MySQL Database include "../storescripts/connect_to_mysql.php"; $sql=mysql_query("SELECT id FROM admin WHERE username='$manager' AND password='$password' LIMIT 1"); //---------MAKE SURE PERSON EXISTS IN DATABASE--------- $existCount=mysql_num_rows($sql);//count the row nums if($existCount==1){//evaluate the count while($row=mysql_fetch_array($sql)){ $id=$row["id"]; } $_SESSION["id"]=$id; $_SESSION["manager"]=$manager; $_SESSION["password"]=$password; header("location:index.php"); exit(); }else{ echo 'That information is incorrect, try again <a href="index.php"> Click Here </a>'; exit(); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/244036-error-help/ Share on other sites More sharing options...
TeNDoLLA Posted August 6, 2011 Share Posted August 6, 2011 This means your SQL query fails and it does not return the resultset. You should add "or die(mysql_error())" after you run the query to see the error it gives. Or you could also echo the query before you run it and see it if there is false syntax in it. Quote Link to comment https://forums.phpfreaks.com/topic/244036-error-help/#findComment-1253228 Share on other sites More sharing options...
haris244808 Posted August 6, 2011 Author Share Posted August 6, 2011 Actually it returns empty result (MySQL returned an empty result set (i.e. zero rows). Quote Link to comment https://forums.phpfreaks.com/topic/244036-error-help/#findComment-1253233 Share on other sites More sharing options...
haris244808 Posted August 6, 2011 Author Share Posted August 6, 2011 bool(false) Query was empty Quote Link to comment https://forums.phpfreaks.com/topic/244036-error-help/#findComment-1253235 Share on other sites More sharing options...
TeNDoLLA Posted August 6, 2011 Share Posted August 6, 2011 Do this before running query echo "SELECT id FROM admin WHERE username='$manager' AND password='$password' LIMIT 1"; Paste results, there you may see that some things in the query is not like you wanted them to be. Quote Link to comment https://forums.phpfreaks.com/topic/244036-error-help/#findComment-1253238 Share on other sites More sharing options...
haris244808 Posted August 6, 2011 Author Share Posted August 6, 2011 I executed from phpmyadmin and pasted results before it was: MySQL returned an empty result set (i.e. zero rows) from browser the resultswas: bool(false) Query was empty Quote Link to comment https://forums.phpfreaks.com/topic/244036-error-help/#findComment-1253241 Share on other sites More sharing options...
TeNDoLLA Posted August 6, 2011 Share Posted August 6, 2011 Well then there is no matching row for the $manager and $password you are providing for the query. To avoid the error you check if it is false before you do anything with the "resulset". Quote Link to comment https://forums.phpfreaks.com/topic/244036-error-help/#findComment-1253244 Share on other sites More sharing options...
haris244808 Posted August 6, 2011 Author Share Posted August 6, 2011 But i have a table admin which contains id, username,password and last_log_date , how is possible to return empty result :S Quote Link to comment https://forums.phpfreaks.com/topic/244036-error-help/#findComment-1253254 Share on other sites More sharing options...
PFMaBiSmAd Posted August 6, 2011 Share Posted August 6, 2011 If you are getting an error message "Query was empty" from mysql_error(), it means that your php code is not supplying any sql query at all to the mysql_query() statement. You either have a wrongly named variable or an empty string and the code you posted at that start of this thread cannot produce a "Query was empty" error. Post the actual code forming the sql query and executing the mysql_query() statement that is producing that error message. Quote Link to comment https://forums.phpfreaks.com/topic/244036-error-help/#findComment-1253255 Share on other sites More sharing options...
haris244808 Posted August 6, 2011 Author Share Posted August 6, 2011 here is index.php: <?php session_start(); if(!isset($_SESSION["manager"])){ header("location:admin_login.php"); exit(); } //Be sure to check that this manager SESSION value is in fact in the database $manager=preg_replace('#[A-Za-z0-9]#i','',$_POST["username"]);//Filter everything but numbers and letters $password=preg_replace('#[A-Za-z0-9]#i','',$_POST["password"]);//Filter everything but numbers and letters //Run MySQL query to be sure that this person is an admin and that their password session var equals the database information //Connect to MySQL Database include "../storescripts/connect_to_mysql.php"; $sql=mysql_query("SELECT * FROM admin WHERE id='$managerID' AND username='$manager' AND password='password' LIMIT 1"); //---------MAKE SURE PERSON EXISTS IN DATABASE--------- $existCount=mysql_num_rows($sql);//count the row nums if($existCount==0){//evaluate the count echo "Your login session data is not on record on database."; exit(); } ?> and here is admin_login.php: <?php session_start(); if(isset($_SESSION["manager"])){ header("location:index.php"); exit(); } ?> <?php //Parse the log in form if the user has filled out and pressed "Log In" if(isset($_POST["username"])&&($_POST["password"])){ $manager=preg_replace('#[A-Za-z0-9]#i','',$_POST["username"]);//Filter everything but numbers and letters $password=preg_replace('#[A-Za-z0-9]#i','',$_POST["password"]);//Filter everything but numbers and letters //Connect to MySQL Database include "../storescripts/connect_to_mysql.php"; $sql=mysql_query("SELECT id FROM admin WHERE username='$manager' AND password='$password' LIMIT 1"); //---------MAKE SURE PERSON EXISTS IN DATABASE--------- $existCount=mysql_num_rows($sql);//count the row nums if($existCount==1){//evaluate the count while($row=mysql_fetch_array($sql)){ $id=$row["id"]; } $_SESSION["id"]=$id; $_SESSION["manager"]=$manager; $_SESSION["password"]=$password; header("location:index.php"); exit(); }else{ echo 'That information is incorrect, try again <a href="index.php"> Click Here </a>'; exit(); } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Admin Login</title> <link rel="stylesheet" href="../style/style.css" type="text/css" media="screen" /> </head> <body> <div align="center" id="mainWrapper"> <?php include_once("../template_header.php");?> <div id="pageContent"><br/> <div align="left" style="margin-left:24px;"> <h2>PLease Log In to Manage the Store</h2> <form id="form1" name="form1" method="post" action="admin_login.php"> User Name:<br/> <input name="username" type="text" id="username" size="40" /> <br/><br/> Password:<br/> <input name="password" type="password" id="password" size="40" /> <br/><br/><br/> <input type="submit" name="button" id="button" value="Log In" /> </form> </div> <br/> <br/> </div> <?php include_once("../template_footer.php");?> </div> </body> </html> Database have a table admin that contains: username, password and last_log_date Quote Link to comment https://forums.phpfreaks.com/topic/244036-error-help/#findComment-1253262 Share on other sites More sharing options...
PFMaBiSmAd Posted August 6, 2011 Share Posted August 6, 2011 Neither code you just posted contains any logic that has an added "or die(mysql_error())" on the end of the mysql_query() statement that would be outputting a "Query was empty" message. Your original problem, the "mysql_num_rows() expects parameter 1 to be resource, boolean given" error message is because your query failed to execute at all due to an error of some kind (a connection problem, a sql syntax problem, a misnamed table or column.) Adding the "or die(mysql_error())" that TeNDoLLA suggested, on to the end of your mysql_query() statement would have told you why the query is failing. If you actually added that and you got a "Query was empty" message, it didn't come from the code you have been posting. If the "Query was empty" message is your interpretation of what you have seen in front of you, sorry but we cannot help unless you post actual error messages and actual code that produces those error messages. Quote Link to comment https://forums.phpfreaks.com/topic/244036-error-help/#findComment-1253269 Share on other sites More sharing options...
haris244808 Posted August 6, 2011 Author Share Posted August 6, 2011 i did that and showed me that the query is empty here i also upload a sql syntax of admin table: CREATE TABLE admin ( id int(11) NOT NULL AUTO_INCREMENT, username varchar(24) NOT NULL, password varchar(24) NOT NULL, last_log_date date NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY username (`username`) ) the index page and tha admin_login page i added before: This is the original result without or die (mysql_error()) Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\MyOnlineStore\storeadmin\admin_login.php on line 17 and with or die(mysql_error()) function it says: query is empty Quote Link to comment https://forums.phpfreaks.com/topic/244036-error-help/#findComment-1253297 Share on other sites More sharing options...
haris244808 Posted August 6, 2011 Author Share Posted August 6, 2011 with mysql_error() funcrion ...now says NO database selected Now i did all the things and i came up that somehow skips the sql statement it directly shows me: That information is incorrect, try again Click Here line (you can see this in admin_login page that i posted) ANy sugesstion?? Quote Link to comment https://forums.phpfreaks.com/topic/244036-error-help/#findComment-1253302 Share on other sites More sharing options...
haris244808 Posted August 6, 2011 Author Share Posted August 6, 2011 Thank you for the replyes and the time. I just found the mistake... i forgot to filter the managerID variable Quote Link to comment https://forums.phpfreaks.com/topic/244036-error-help/#findComment-1253307 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.