我试图从数据库表中显示数量值,但它没有在浏览器中显示$quantity
值。为什么呢?
<?php
include('connection.php');
# cartexe.php will perform all actions on cart.php
// add Item to Cart
if(isset($_POST['addItemToCart']))
{
// initialize index.php variables
$productName = $_POST['productName'];
$price = $_POST['price'];
$description = $_POST['description'];
$quantity = 0;
// check the cart table to see if the product has been previously added
$smtp = $conn->prepare('SELECT * FROM cart WHERE Product_Name = :product'); // prepare a statement to fetch the quanity for a particular pprodut... the statement is not executed but merely prepared to do so.
$smtp->bindParam(':product', $productName, PDO::PARAM_STR); //bind the productNAme with whatever data supplied hence the statement after : above will be replaced with the actually data.. In additional the statement is set as string hence PDO::PRAM_STR
$smtp->execute();//finally run the statment
//var_dump($smtp);
//find the number of rows affected
$result = $smtp->fetch(PDO::FETCH_NUM);
// var_dump($smtp);
//the data does existed
if($result > 0)
{
$row = $smtp->fetch(PDO::FETCH_ASSOC);
echo " DATA FOUND";
echo $row['Quantity'];
//$quantity++; // increment the quanity if data found
}
//no match found
else
{
echo ' No data found';
}
}
?>
答案 0 :(得分:1)
您似乎不需要先尝试尝试
$smtp = $conn->prepare('SELECT * FROM cart WHERE Product_Name = :product'); // prepare a statement to fetch the quanity for a particular pprodut... the statement is not executed but merely prepared to do so.
$smtp->bindParam(':product', $productName, PDO::PARAM_STR); //bind the productNAme with whatever data supplied hence the statement after : above will be replaced with the actually data.. In additional the statement is set as string hence PDO::PRAM_STR
;//finally run the statment
//the data does existed
if( $smtp->execute() )
{
$row = $smtp->fetch(PDO::FETCH_ASSOC);
echo " DATA FOUND";
echo $row['Quantity'];
//$quantity++; // increment the quanity if data found
}
答案 1 :(得分:1)
$result = $smtp->fetch(PDO::FETCH_ASSOC); // returns result
$result = $smtp->fetch(PDO::FETCH_ASSOC); // returns false
所以,一个解决方案就是
$result = $smtp->fetch(PDO::FETCH_ASSOC);
if($result != false){
//....