I am trying to do a shopping cart in php.
I am getting the following error
"Warning: mysql_num_rows() expects parameter 1 to be resource, string given in C:\\wamp\\www\\shoppingCart\\cart.php on line 19" and its returning there is no products.
The error in the code is in line 19:
if(mysql_num_rows($get)==0)[
<?php
session_start();
$page='index.php';
$server="localhost";
$username="root";
$password="";
$database="jennifer_db";
$conn = new mysqli($server, $username, $password, $database);
if($conn->connect_error){
die("Connection failed: " .$conn->Connect_error);
}
function products(){
$get='mysql_query("select Product_ID, Product_name, Product_desc,
Product_price from products where quantity>0 order by id desc")';
if(mysql_num_rows($get)== 0){
echo "There ae no products to display!";
}
else{
echo "Success!";
}
}
?>
You are mixing mysqli
and mysql
functions. Please use mysqli
or PDO
I made some modifications in your code (I used mysqli), I think it should work, check out the comments:
session_start();
$page = 'index.php';
$server = 'localhost';
$username = 'root';
$password = '';
$database = 'jennifer_db';
// this is the connection object
$mysqli = new mysqli($server, $username, $password, $database);
// if we have an error we abort
if ($mysqli->connect_errno)
die('Connection failed: ' . $mysqli->connect_error);
function products(&$conn)
{
// we pass $conn to this function and make the query
// note that only the SQL query string should be in ""
$result = $conn->query("select Product_ID, Product_name, Product_desc, Product_price from products where quantity > 0 order by id desc");
if ($result)
{
// checking row count
echo (!$result->num_rows ? 'There are no products to display!' : 'Success');
// we close the result set
$result->close();
}
else
echo "Error: {$conn->error}";
}
// call products() here
products($mysqli);
// we close the connection
$conn->close();
For further info about mysqli check http://php.net/manual/en/book.mysqli.php
also check out PDO and decide which one you want to use (mysqli or PDO) http://php.net/manual/en/book.pdo.php
I think you can learn from this too: http://davidwalsh.name/php-shorthand-if-else-ternary-operators
Hope I helped. :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.