0

我需要一个函数来从查询中检索数据并将该信息发送到要求加载表(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>

细心的建议和帮助!

谢谢!问候

毛里希奥兹

4

2 回答 2

1

您需要将每个对象添加到一个数组中并返回它。将您的 getProducts 函数更改为如下所示:

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";
    $objects = array();
    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"];
            $objects[] = $object;
        }
    }
    $mysqli->close();
    return $objects;
}

此外,正如Mark B在评论中提到的那样,没有真正需要创建一个新对象来存储mysqli_fetch_object调用结果,至少在您当前编写的代码时是这样。所以while循环真的可以简化成这样的:

while ($obj = mysqli_fetch_object($result)) {
    $objects[] = $obj;
}
于 2013-06-24T21:01:13.993 回答
1

您需要将每一行保存到一个数组中。不要忘记你正在生成一个对象数组,你不能用方括号访问它的元素:

class Products
{
    public $Id;
    public $sku;
    public $name;
    public $price;

    var $function_result = array();

    public function getProducts () {
        $products = array();
        $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)) {
            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"];

                $products []= $object;
            }
        }

        $mysqli->close();

        return $products;
    }
}

使用箭头运算符访问对象属性:

require_once 'Clases/productos.class.php';
$almacen = new Productos();
$productos = $almacen->getProducts();

foreach ($productos as $producto) { ?>
    <p><?php echo htmlspecialchars($producto->name)></p>
<?php }
于 2013-06-24T21:08:06.597 回答