Php 我该如何从数据库返回具有实际值的数组?继续保持空白
因此,最终目标是使用这些类中的函数将产品作为表中的对象打印出来,但输出是一个具有正确行/列数的表,但它们是空的。我花了3个小时试图弄明白这一点,我被难倒了。有三个文件 Product.phpPhp 我该如何从数据库返回具有实际值的数组?继续保持空白,php,mysql,Php,Mysql,因此,最终目标是使用这些类中的函数将产品作为表中的对象打印出来,但输出是一个具有正确行/列数的表,但它们是空的。我花了3个小时试图弄明白这一点,我被难倒了。有三个文件 Product.php <?php class Product { protected $product_id; protected $product_name; protected $product_price; public function __construct($produc
<?php
class Product
{
protected $product_id;
protected $product_name;
protected $product_price;
public function __construct($product_id,$product_name, $product_price = '')
{
$this->setProductID($product_id);
$this->setProductName($product_name);
$this->setProductPrice($product_price);
}
public function getProductID() {
return $this->product_id;
}
public function getProductName() {
return $this->product_name;
}
public function getProductPrice() {
return $this->product_price;
}
public function setProductID($product_id)
{
$this->product_id = $product_id;
}
public function setProductName($product_name)
{
$this->product_name= $product_name;
}
public function setProductPrice($product_price)
{
$this->product_price= $product_price;
}
}
ProductMapper.php
<?php
class ProductMapper
{
public function getProducts()
{
$dbConn = getDbConnection();
$stmt = $dbConn->prepare("SELECT * FROM product");
$stmt->execute();
$outArray = array();
while ($row = $stmt->fetch()) {
$outArray[] = new Product($row['product_id'], $row['product_name'], $row['product_cost']);
}
return $outArray;
}
}
以及Index.php,这是它的输出位置
<?php
require('classes/functions.php');
require('classes/Product.php');
require('classes/ProductMapper.php');
$productMapper = new ProductMapper();
$rows = $productMapper->getProducts();
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Product</title>
</head>
<body>
<tbody>
<?php
foreach ($rows as $row) {
echo "<tr>";
echo "<td>{$row->getProductID($this)}</td>";
echo "<td>{$row->getProductName($id)}</td>";
echo "<td>{$row->getProductPrice($id)}</td>";
echo "</tr>";
}
?>
</tbody>
</table>
</body>
</html>
您提供的代码存在一些小问题,这些问题会阻止它工作。您是否设置了“错误报告” index.php: “getProduct”方法不接受任何参数。因此,不应使用任何参数调用它们。 ProductMapper.php: 为了保持一致,我将“产品成本”列重命名为“产品价格”。 我在构造函数中注入了数据库连接,并将其作为属性。为将来的任何方法提供连接。 一些示例输出:
pid01 name-pid01 23.34
pid02 name-pid02 101.67
这里是新的脚本
index.php
require('classes/functions.php');
require('classes/Product.php');
require('classes/ProductMapper.php');
$productMapper = new ProductMapper(getDbConnecton());
$rows = $productMapper->getProducts();
// note: the 'getProductID', 'getProductName' and 'getProductPrice' methods
// do not accept or need, any parameters.
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Product</title>
</head>
<body>
<table>
<tbody>
<?php
foreach ($rows as $row) {
echo "<tr>";
echo "<td>{$row->getProductID()}</td>";
echo "<td>{$row->getProductName()}</td>";
echo "<td>{$row->getProductPrice()}</td>";
echo "</tr>";
}
?>
</tbody>
</table>
是否有理由将值发送回循环中的产品构造函数?
// ProductMapper.php
class ProductMapper
{
/**
* @var PDO
*/
private $dbConn = null;
public function __construct(PDO $pdo)
{
$this->dbConn = $pdo;
}
public function getProducts()
{
$dbConn = $this->dbConn;
$stmt = $dbConn->prepare("SELECT * FROM product");
$stmt->execute();
$outArray = array();
while ($row = $stmt->fetch()) {
$outArray[] = new Product($row['product_id'], $row['product_name'], $row['product_price']);
}
return $outArray;
}
}