大家好,我需要您就以下编码方式是否可以接受以及在 3 件事方面更好的编码方式(检索数据、初始化变量和回显它)提出建议。
class Product {
private $product_name = array();
private $num_rows;
public function __construct() {
include 'connect.php';
$query = "SELECT * FROM product where product_status= 'open' ";
$result = mysql_query($query);
$this->num_rows = mysql_num_rows($result);
while ($record = mysql_fetch_array($result)) {
$this->product_name[] = $record['product_name'];
}
}
public function get_rows() {
return $this->num_rows;
}
public function get_name($count) {
return $this->product_name[$count];
}
}
$product = new Product();
$rows = $product->get_rows();
for ($i = 0; $i < $rows; $i++) {
echo $product->get_name($i);
}