Jump to content

Warning: mysql_num_rows() expects parameter 1 to be resource


ZeTutorials101

Recommended Posts

Hey guys - I've started coding a simple PHP mini shopping cart script, and I'm getting this error: 

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

My Code is the following

session_start();
$page = 'index.php';
require('config.php');


function products(){
$get = mysql_query('SELECT id, name, description, price FROM products WHERE quantity > 0 ORDER BY id DESC');
$get1 = mysql_num_rows($get);
if ($get1==0){
echo "There are no products for sale at this moment, please check back later.";
} else {
echo "Success!"; //echo products
}
}
mysql_close();

 

Any idea why i'm getting this error? How can I fix it?

 

Thanks.

Edited by ZeTutorials101
Link to comment
Share on other sites

You are getting an error. Change the line that executes the query to this

 

$query = 'SELECT id, name, description, price FROM products WHERE quantity > 0 ORDER BY id DESC';
$get = mysql_query($query) or die("Query: $query<br>Error: " . mysql_error());

 

Note: putting the query into a sting variable is useful so you can echo the query to the page for debugging purposes. Here,the query is hard-coded (i.e. no variables) so it isn't really necessary, but it is a good habit to follow.

Link to comment
Share on other sites

@Psycho, Now i'm getting this:

Query: SELECT id, name, description, price FROM products WHERE quantity > 0 ORDER BY id DESC
Error: No database selected

EDIT: Never mind - It was because I didn't include the config file In the Function.

Edited by ZeTutorials101
Link to comment
Share on other sites

You shouldnt have to include the file in the function as long as its included on the file that calls the function.

Well, no actually. Commonly people code mysql connections :  

$con = mysql_connect('host', 'user', 'pass');
$db = mysql_select_db('databaseName', $con);
...

When you do this you assign the connection and database resources to variables, and as such when you enter into a function there is no visibility of it. Passing the connection variables into the function would obviously fix this so that, as you say, you would not need to call the include within the function, but your statement in and of it's self is not strictly accurate. 

Link to comment
Share on other sites

Well, no actually. Commonly people code mysql connections :  

$con = mysql_connect('host', 'user', 'pass');
$db = mysql_select_db('databaseName', $con);
...

When you do this you assign the connection and database resources to variables, and as such when you enter into a function there is no visibility of it. Passing the connection variables into the function would obviously fix this so that, as you say, you would not need to call the include within the function, but your statement in and of it's self is not strictly accurate. 

 

That is not accurate when using the mysql_query() function. It depends on "how" you are using that connection. Using the connection as you've coded it above does not require that the function has "visibility" of them. As long as a database connection was made outside the function, you can execute a query in the function without passing those references into the function (or, god forbid, using GLOBAL). Because, as stated in the manual, the second parameter for mysql_query() (the resource link identifier) is optional.

 

 

If the link identifier is not specified, the last link opened by mysql_connect() is assumed.

 

If, however, you are using mysqli then it is a little different. Typically, I would think, you would create an object for the connection and then pass that object to the function. But, I think you could run a query in the function without passing any references if you executed the query using this format:

 

 

mysqli::query ( string $query [, int $resultmode = MYSQLI_STORE_RESULT ] )
Link to comment
Share on other sites

hmm, so the issue being fixed by including the declaration of the connection would have some other source.

 

I apologise - I didn't realise mysql_ was able to break scope quite that badly (never used it in any circumstance that would demonstrate it).  I don't know about mysqli_ at all, I jumped straight to PDO from mysql_ when the depreciation notice came about.

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.