Trying to use the while loop
to echo the prod_qty
and prod_desc
fields from database. Getting no errors. I've also tried using the foreach loop
as you can see in the code, yet it doesn't display anything.
Here is the cart.php file:
<?php
// 1. Establish a connection using three functions: 1. mysqli_connect() 2. mysqli_connect_errno() 3. mysqli_connect_error()
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "shoppingcart";
$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
// Test if connection occured
if(mysqli_connect_errno()) {
die("Database connection failed " . mysqli_connect_error() . "( " . mysqli_connect_errno() . " )");
}
// Show the products by performing a query to database.
$query = "SELECT prod_desc, prod_qty FROM cart WHERE prod_id = 1 AND prod_id = 2 AND prod_id = 3 ";
$result = mysqli_query($connection, $query);
while($cart = mysqli_fetch_assoc($result)) {
echo $cart["prod_qty"];
echo $cart["prod_desc"];
}
/*if($result) {
foreach ($result as $cart ) {
echo $cart["prod_qty"];
echo $cart["prod_desc"];
}
} */
?>
Here is my database structure:
I'm sure I am making a silly mistake because I got no errors! Any ideas guys?
If you wanted to get records having prod_id
1 or 2 or 3 you can use in
as below :
$query = "SELECT prod_desc, prod_qty FROM cart WHERE prod_id in (1, 2, 3 )";