我需要一個函數來從查詢中檢索數據並將該信息發送到請求加載表(html)的頁面,我正在嘗試所見,但沒有得到結果。如何從PHP5.3中的查詢中獲取對象數組?
CLASS PHP
class products {
public $Id;
public $sku;
public $name;
public $price;
var $function_result = array();
function getProducts() {
$mysqli = new mysqli("localhost", "root", "usbw", "Datos");
if (mysqli_connect_errno()) {
printf("Error in conection: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT Id,sku,name,price FROM products";
if ($result = $mysqli->query($query)) {
$i = 0;
while ($obj = mysqli_fetch_object($result)) {
$object = new stdClass;
$object->Id = $obj["Id"];
$object->sku = $obj["sku"];
$object->name = $obj["name"];
$object->price = $obj["price"];
}
}
$mysqli->close();
return $object;
}
}
HTML(PHP)
<?php
require 'Clases/productos.class.php';
$pro = new productos();
$datos = $pro->getProducts();
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<?php
for ($i = 0; $i < sizeof($datos); $i++) {
?>
<p><?php echo $datos[$i]["name"]; ?></p>
<?php
}
?>
</body>
</html>
周到的建議和幫助!
謝謝! 問候
mauriciohz
和問題是什麼? – 2013-06-24 21:00:40