变量不显示它的输出 php

variable not displaying it's output php

给出这个 php 块:

<?php
//This block grabs whole list for viewing
$product_list = "";
$db = new PDO('mysql:host=mysql9.000webhost.com;dbname=a9802737_mystore', 'a9802737_mystore', 'mystore1');
$sql= $db->prepare("SELECT COUNT(*) FROM products ORDER BY date_added DESC");
$sql->execute();
$productCount = $sql->fetchColumn(); //count the output amount
if($productCount>0){

             while($row = $sql->fetch()){ 
                 $id = $row["id"];
                 $product_name = $row["product_name"];
                 $price = $row["productPrice"];
                 $date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
                 $product_list .= "Product ID: $id - <strong>$product_name</strong> - $$price - <em>Added $date_added</em> &nbsp; &nbsp; &nbsp; <a href='inventory_edit.php?pid=$id'>edit</a> &bull; <a href='inventory_list.php?deleteid=$id'>delete</a><br/>";


             }



} else{
    $product_list = "You have no products listed in your store yet.";
}

?>

如您所见,我先声明了 $product_list 并用空白字符串对其进行了初始化,然后在 while 循环中更新了它的值。我在 php 块

中再次调用了 $product_list

<div align = "right" style="margin-right:32px;"><a href="#inventory_list.php#inventoryForm">+ Add New T-Shirt</a></div>
<div align="left"style="margin-left:24px">
  <h2>Inventory List</h2>
    <?php echo $product_list; ?>
    <p></p>
</div>

它显示的值是“”(空白)字符串而不是更新后的字符串。

什么时候应该是这样的:

问题是您是 运行 查询,它只选择了项目数:

$sql= $db->prepare("SELECT COUNT(*) FROM products ORDER BY date_added DESC");

因此,fetchColumn() 方法 return 产品数量,但 fetch() 没有 return 产品。 要获取结果,您应该这样做:

$sql= $db->query("SELECT * FROM products ORDER BY date_added DESC");
while($row = $sql->fetch()){ 
                 $id = $row["id"];
                 $product_name = $row["product_name"];
                 $price = $row["productPrice"];
                 $date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
                 $product_list .= "Product ID: $id - <strong>$product_name</strong> - $$price - <em>Added $date_added</em> &nbsp; &nbsp; &nbsp; <a href='inventory_edit.php?pid=$id'>edit</a> &bull; <a href='inventory_list.php?deleteid=$id'>delete</a><br/>";


             }

试试这个:

<?php
//This block grabs whole list for viewing
$product_list = "";
$db = new PDO('mysql:host=mysql9.000webhost.com;dbname=a9802737_mystore', 'a9802737_mystore', 'mystore1');

$sql = $db->prepare("SELECT * FROM products ORDER BY date_added DESC");
$sql->execute();

if($sql->rowCount()) {

    while($row = $sql->fetch()) { 
        $id = $row["id"];
        $product_name = $row["product_name"];
        $price = $row["productPrice"];
        $date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
        $product_list .= "Product ID: $id - <strong>$product_name</strong> - $$price - <em>Added $date_added</em> &nbsp; &nbsp; &nbsp; <a href='inventory_edit.php?pid=$id'>edit</a> &bull; <a href='inventory_list.php?deleteid=$id'>delete</a><br/>";
    }
} else {
    $product_list = "You have no products listed in your store yet.";
}

?>

这里

if ($sql->rowCount()) {
    //
}

条件将检查查询returns是否有任何结果。