MauricioHz
MauricioHz

Reputation: 111

how to get an array of objects from a query in PHP5.3?

I need a function to retrieve data from a query and send that information to the page that asks to load a table (html), I'm trying what you see, but do not get to have a result.

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>

attentive to the suggestions and help!

Thank you! regards

mauriciohz

Upvotes: 0

Views: 180

Answers (2)

James Holderness
James Holderness

Reputation: 23021

You need to add each object into an array and return that. Change your getProducts function to something like this:

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;
}

Also, as Mark B has mentioned in the comments, there's no real need to create a new object to store the result from the mysqli_fetch_object call, at least as your code is currently written. So the while loop could really be simplified to something like this:

while ($obj = mysqli_fetch_object($result)) {
    $objects[] = $obj;
}

Upvotes: 1

Ast Derek
Ast Derek

Reputation: 2729

You need to save each row into an array. Don't forget you are generating an array of objects, you cannot access its elements with the square brackets:

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;
    }
}

Use the arrow operator to access the object properties:

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

foreach ($productos as $producto) { ?>
    <p><?php echo htmlspecialchars($producto->name)></p>
<?php }

Upvotes: 1

Related Questions