Jump to content

Error Help


haris244808

Recommended Posts

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();
		}
	}
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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??

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.