1,105,320 Community Members

mysql_num_rows() expects parameter 1 to be resource,

Member Avatar
brants91
Light Poster
40 posts since Mar 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
svilla
Junior Poster in Training
91 posts since Aug 2010
Reputation Points: 17 [?]
Q&As Helped to Solve: 28 [?]
Skill Endorsements: 0 [?]
 
0
 
Member Avatar
minitauros
Practically a Master Poster
606 posts since Apr 2011
Reputation Points: 67 [?]
Q&As Helped to Solve: 105 [?]
Skill Endorsements: 6 [?]
 
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.

Member Avatar
karthik_ppts
Posting Pro
574 posts since Feb 2011
Reputation Points: 81 [?]
Q&As Helped to Solve: 101 [?]
Skill Endorsements: 1 [?]
 
0
 

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
Member Avatar
brants91
Light Poster
40 posts since Mar 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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!

Question Answered as of 2 Years Ago by minitauros, svilla and karthik_ppts
Member Avatar
ko ko
Practically a Master Poster
673 posts since Jan 2009
Reputation Points: 97 [?]
Q&As Helped to Solve: 152 [?]
Skill Endorsements: 1 [?]
 
0
 

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

Member Avatar
shuleen
Newbie Poster
1 post since Oct 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
karthik_ppts
Posting Pro
574 posts since Feb 2011
Reputation Points: 81 [?]
Q&As Helped to Solve: 101 [?]
Skill Endorsements: 1 [?]
 
0
 

Start as new thread

Member Avatar
nandkishor.sharma.372_1
Newbie Poster
1 post since Aug 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

@ $db = mysql_connect('localhost', 'root');

do this ........and your probem gets solved

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: