ZeTutorials101 Posted May 20, 2013 Share Posted May 20, 2013 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. Link to comment https://forums.phpfreaks.com/topic/278218-warning-mysql_num_rows-expects-parameter-1-to-be-resource/ Share on other sites More sharing options...
requinix Posted May 20, 2013 Share Posted May 20, 2013 The query failed. Run it manually and see what errors you get. Link to comment https://forums.phpfreaks.com/topic/278218-warning-mysql_num_rows-expects-parameter-1-to-be-resource/#findComment-1431246 Share on other sites More sharing options...
ZeTutorials101 Posted May 20, 2013 Author Share Posted May 20, 2013 I did that as soon as I got the error, but in phpmyadmin it has no error. Link to comment https://forums.phpfreaks.com/topic/278218-warning-mysql_num_rows-expects-parameter-1-to-be-resource/#findComment-1431252 Share on other sites More sharing options...
Barand Posted May 20, 2013 Share Posted May 20, 2013 Have you tried connecting the server/database in your code? Link to comment https://forums.phpfreaks.com/topic/278218-warning-mysql_num_rows-expects-parameter-1-to-be-resource/#findComment-1431257 Share on other sites More sharing options...
ZeTutorials101 Posted May 20, 2013 Author Share Posted May 20, 2013 Yep, that was the first thing I did when making the config.php - I tested it and It worked. Link to comment https://forums.phpfreaks.com/topic/278218-warning-mysql_num_rows-expects-parameter-1-to-be-resource/#findComment-1431259 Share on other sites More sharing options...
Psycho Posted May 20, 2013 Share Posted May 20, 2013 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 https://forums.phpfreaks.com/topic/278218-warning-mysql_num_rows-expects-parameter-1-to-be-resource/#findComment-1431260 Share on other sites More sharing options...
ZeTutorials101 Posted May 20, 2013 Author Share Posted May 20, 2013 Okay, its getting late here - so I will test it tomorow morning. Thanks for the help and I hope it works. Link to comment https://forums.phpfreaks.com/topic/278218-warning-mysql_num_rows-expects-parameter-1-to-be-resource/#findComment-1431261 Share on other sites More sharing options...
ZeTutorials101 Posted May 21, 2013 Author Share Posted May 21, 2013 @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. Link to comment https://forums.phpfreaks.com/topic/278218-warning-mysql_num_rows-expects-parameter-1-to-be-resource/#findComment-1431309 Share on other sites More sharing options...
DaveyK Posted May 21, 2013 Share Posted May 21, 2013 You shouldnt have to include the file in the function as long as its included on the file that calls the function. Link to comment https://forums.phpfreaks.com/topic/278218-warning-mysql_num_rows-expects-parameter-1-to-be-resource/#findComment-1431310 Share on other sites More sharing options...
Muddy_Funster Posted May 21, 2013 Share Posted May 21, 2013 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 https://forums.phpfreaks.com/topic/278218-warning-mysql_num_rows-expects-parameter-1-to-be-resource/#findComment-1431352 Share on other sites More sharing options...
Psycho Posted May 21, 2013 Share Posted May 21, 2013 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 https://forums.phpfreaks.com/topic/278218-warning-mysql_num_rows-expects-parameter-1-to-be-resource/#findComment-1431373 Share on other sites More sharing options...
Muddy_Funster Posted May 21, 2013 Share Posted May 21, 2013 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 https://forums.phpfreaks.com/topic/278218-warning-mysql_num_rows-expects-parameter-1-to-be-resource/#findComment-1431378 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.