鉴于这个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> <a href='inventory_edit.php?pid=$id'>edit</a> • <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>
&#13;
它显示的值是&#34;&#34;(空白)字符串而不是更新后的字符串。
什么时候看起来像这样:
答案 0 :(得分:1)
问题是您正在运行查询,它只选择项目数:
$sql= $db->prepare("SELECT COUNT(*) FROM products ORDER BY date_added DESC");
因此,fetchColumn()
方法返回产品数量,但fetch()
不返回商品。
要获取结果,您应该这样做:
$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> <a href='inventory_edit.php?pid=$id'>edit</a> • <a href='inventory_list.php?deleteid=$id'>delete</a><br/>";
}
答案 1 :(得分:1)
试试这个:
<?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> <a href='inventory_edit.php?pid=$id'>edit</a> • <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()) {
//
}
条件将检查天气查询是否返回任何结果。