如何从PHP5.3中的查询中获取对象数组?

时间:2013-06-24 20:57:47

标签: php

我需要一个函数来从查询中检索数据并将该信息发送到要求加载表(html)的页面,我正在尝试你所看到的,但是没有得到结果。

  

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

2 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分: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 }