Php 为什么';数量';值不显示任何值?

Php 为什么';数量';值不显示任何值?,php,mysql,pdo,Php,Mysql,Pdo,我试图显示数据库表中的数量值,但它没有在浏览器中显示$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 = $_PO

我试图显示数据库表中的数量值,但它没有在浏览器中显示
$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';
     }

 }

 ?>


您似乎不需要先尝试

$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
     }

看起来PDO不允许您获取两次,它只在第一次返回结果,然后返回false

$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){
//....

它是否回显未找到的数据?它回显已找到的数据!在
echo$row['Quantity']之后添加
变量转储($row,$productName)让我们看看您得到了什么。@BojanT
bool(false)string(7)“product”
非常感谢,非常感谢您的解决方案!