Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you use PHP 5.3.0 and mysqlnd, you can use the new function <a href="http://php.net/manual/en/mysqli-result.fetch-all.php" rel="nofollow noreferrer"><code>mysqli_fetch_all()</code></a>. This returns an array of associative arrays.</p> <p>If you use an earlier version of PHP, you could switch to using PDO, and use the function <a href="http://php.net/manual/en/pdostatement.fetchall.php" rel="nofollow noreferrer"><code>PDOStatement::fetchAll()</code></a>.</p> <p>You ask in a comment what about a very large result set. It's true that an unbounded result set could cause the array to exceed your PHP memory limit and that would cause a fatal error and halt the script. But is this really a problem? How many products do you have? You could use <code>LIMIT</code> to make sure the query isn't unbounded.</p> <hr> <p>Re the other part of your questions regarding going back to a class, I'd suggest making an <a href="http://php.net/iterator" rel="nofollow noreferrer">Iterator</a> class:</p> <pre><code>class DB implements IteratorAggregate { protected $_data = array(); public function getProductList() { // fetch all results from SQL query, stuff them into $this-&gt;_data return $this-&gt;getIterator(); } public function getIterator() { return new ArrayIterator($this-&gt;_data); } } </code></pre> <p>Now you can use the class in a <code>foreach</code> loop:</p> <pre><code>$db = new DB(); foreach ($db-&gt;getProductList() as $product) { // do something with each product } </code></pre> <p>The <code>IteratorAggregate</code> interface means you can even do this:</p> <pre><code>$db = new DB(); $db-&gt;getProductList(); // ...other steps... foreach ($db as $product) { // do something with each product } </code></pre> <p>Of course you could only store one result set at a time with this method. If you used your DB class for any other queries in the meantime, it would complicate things. For this reason, most people don't try to write a single class to encapsulate all database operations. They write individual classes for each type of <a href="http://martinfowler.com/eaaCatalog/domainModel.html" rel="nofollow noreferrer">Domain Model</a> they need to work with, decoupled from the database connection.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload