[PHP-NOTES] note 130591 added to mysqli-result.fetch-assoc

[email protected] ("[email protected]") Fri, 19 Dec 2025 07:37:09 +0000
Newsgroups php.notes
Message-ID <[email protected]>
here's an example of a repository using fetch :

<?php
namespace app\model\repository;
class DbProductRepository implements ProductRepositoryInterface
{
    private $connexion;
    public function __construct()
    {
        $dsn = "sqlite:" . CFG["db"]["host"] . CFG["db"]["database"];
        $this->connexion = \system\SPDO::getInstance(
            $dsn,
            CFG["db"]["login"],
            CFG["db"]["password"],
            CFG["db"]["options"],
            CFG["db"]["exec"]
        )
            ->getConnexion();
    }
    public function findAll(): array
    {
        $sql = "SELECT * FROM product";
        $stmt = $this->connexion->prepare($sql);
        $stmt->execute();
        return $stmt->fetchAll(\PDO::FETCH_ASSOC);
    }
    public function add(
        \app\model\entity\ProductEntity $product
    ): \app\model\entity\ProductEntity {
        $sql = "INSERT INTO product (id, name, price, quantity) VALUES ({$product->getId()}, '{$product->getName()}'    , {$product->getPrice()}, {$product->getQuantity()})";
        $stmt = $this->connexion->prepare($sql);
        $stmt->execute();
        return $product;
    }
    public function findById(int $id): ?\app\model\entity\ProductEntity
    {
        $products = $this->findAll();
        foreach ($products as $product) {
            if ((int)$product["id"] === $id) {
                $ref = new \ReflectionClass(\app\model\entity\ProductEntity::class);
                $nproduit = $ref->newInstanceWithoutConstructor();
                $nproduit->setId((int)$product['id']);
                $nproduit->setName($product['name']);
                $nproduit->setPrice((float) $product['price']);
                $nproduit->setQuantity((int) $product['quantity']);
                return $nproduit;
            }
        }   
        return null;
    }
    public function update(
        int $idS,
        \app\model\entity\ProductEntity $product
    ): \app\model\entity\ProductEntity {
        $sql = "UPDATE product SET name = '{$product->getName()}', price = {$product->getPrice()}, quantity = {$product->getQuantity()} WHERE id = {$idS}";
        $stmt = $this->connexion->prepare($sql);
        $stmt->execute();
        return $product;
    }
    public function delete(int $id): bool
    {
        $sql = "DELETE FROM product WHERE id = {$id}";
        $stmt = $this->connexion->prepare($sql);
        return $stmt->execute();
    }
}
----
Server IP: 206.189.2.9
Probable Submitter: 2001:660:7220:385:193:52:103:33
----
Manual Page -- https://php.net/manual/en/mysqli-result.fetch-assoc.php
Edit        -- https://main.php.net/note/edit/130591
Del: integrated  -- https://main.php.net/note/delete/130591/integrated
Del: useless     -- https://main.php.net/note/delete/130591/useless
Del: bad code    -- https://main.php.net/note/delete/130591/bad+code
Del: spam        -- https://main.php.net/note/delete/130591/spam
Del: non-english -- https://main.php.net/note/delete/130591/non-english
Del: in docs     -- https://main.php.net/note/delete/130591/in+docs
Del: other reasons-- https://main.php.net/note/delete/130591
Reject      -- https://main.php.net/note/reject/130591
Search      -- https://main.php.net/manage/user-notes.php