0

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

0

2 Answers 2

1

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;
}
Sign up to request clarification or add additional context in comments.

3 Comments

there's very little point in doing all those $object->whatever assignments anyways. fetch_object already returns a stdobject, so $objects[] = $obj is all that should be needed.
@MarcB That's a fair point, but it's also possible this is just a simplified version of the code, and there may be other reasons for wanting to construct a new object.
@MarcB I do think it's worth mentioning though, so I've added your suggestion to the answer. Thanks.
1

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 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.