We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,307 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

mysql_num_rows() expects parameter 1 to be resource,

I get this message quite often and it has popped up again here, any ideas as to why?

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\.... on line 76

line 76 is this line in bold.

$productlist = "";
$sql = mysql_query("SELECT * FROM products ORDER BY dateadded DESC");
[B]$productCount = mysql_num_rows($sql); // count the output amount[/B]
if ($productCount > 0) {
	while($row = mysql_fetch_array($sql)){ 
             $id = $row["id"];
			 $productname = $row["productname"];
			 $price = $row["price"];
			 $dateadded = strftime("%b %d, %Y", strtotime($row["dateadded"]));
			 $productlist .= "Product ID: $id - <strong>$productname</strong> - $price - <em>Added $dateadded</em> &nbsp; &nbsp; &nbsp; <a href='inventoryedit.php?pid=$id'>edit</a> &bull; <a href='inventorylist.php?deleteid=$id'>delete</a><br />";
    }
} else {
	$productlist = "You have no products listed in your store yet";
}

If anyone has the answer for this that will be great.

6
Contributors
7
Replies
5 Months
Discussion Span
1 Year Ago
Last Updated
17
Views
Question
Answered
brants91
Light Poster
40 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
svilla
Junior Poster in Training
91 posts since Aug 2010
Reputation Points: 27
Solved Threads: 28
Skill Endorsements: 0

For as far as I know, executing a COUNT instead of count_num_rows is faster, performance wise.

Your new query would look like

SELECT COUNT(a_unique_field_occuring_in_every_record_that_is_selected) AS num_rows, * FROM products ORDER BY dateadded DESC

I'm not 100% sure if it is faster though, but with this query, you could use mysql_fetch_assoc to fetch your results. $fetch will then be the number of rows.

minitauros
Posting Whiz
346 posts since Apr 2011
Reputation Points: 45
Solved Threads: 57
Skill Endorsements: 4

May be you have mistaken in table name or fieldname. so just echo your query as given below. Copy that query and paste it in phpmyadmin's SQL Section and see the output.

echo $query="SELECT * FROM products ORDER BY dateadded DESC";
$sql = mysql_query($query);
$productCount = mysql_num_rows($sql); // count the output amount
Karthik_pranas
Posting Pro
571 posts since Feb 2011
Reputation Points: 96
Solved Threads: 101
Skill Endorsements: 0

Thanks for your help guys, i solved this yesterday. I checked the entire code and realized that i had forgotten to insert the connect to the mysql database code, it was not actually the code itself!

brants91
Light Poster
40 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 2 Years Ago by svilla, Karthik_pranas and minitauros

Mark this thread 'Solved', if it was solved.

ko ko
Practically a Master Poster
673 posts since Jan 2009
Reputation Points: 120
Solved Threads: 152
Skill Endorsements: 1

i get this message:
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in D:\xamps\xampp\htdocs\register.php on line 19

<?php

include("connection.php");

$username = $_POST['username'];
$email = $_POST['email'];
$password = $_POST['password'];
$repassword = $_POST['password2'];
$phoneno = $_POST['phoneno'];
$address = $_POST['address'];
$postcode = $_POST['postcode'];
$city = $_POST['city'];
$state = $_POST['state'];
$country = $_POST['country'];


$sql = "select * from user where username='".$username."'";
$result = mysql_query($sql) ;
$row = mysql_num_rows($result);  line 19
if($row!= 0)
{
    echo "Register failed. Your username has been used by other member!<br />";
    echo "<a href=\"regframe.php\">Try again</a>";
}else
{

$sql2 = "insert into user('username','email','password','retypepassword','phoneno','address','postcode','city','state','country') values ( '".$username."', '".$email."','".$password."','".$repassword."','".$phoneno."', '".$address."','".$postcode."','".$city."','".$state."','".$country."','user')";
mysql_query($sql2) or die ("Register fail! Please try again later!");
mysql_close($conn);

echo "Thank you for sign up as member<br/>";
echo "Please Login to continue";
echo "<meta http-equiv=\"refresh\"content=\"1;URL=loginpage1.php\">";
}
?>

Please help!! thanks

shuleen
Newbie Poster
1 post since Oct 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Start as new thread

Karthik_pranas
Posting Pro
571 posts since Feb 2011
Reputation Points: 96
Solved Threads: 101
Skill Endorsements: 0

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.1049 seconds using 2.74MB