我需要一个函数来从查询中检索数据并将该信息发送到要求加载表(html)的页面,我正在尝试您所看到的,但没有得到结果。
类 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>
细心的建议和帮助!
谢谢!问候
毛里希奥兹