Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP Using a while loop to fetch items, using a dynamic select function?
    primarykey
    data
    text
    <p>Okay, I've never ever used dynamic functions, not sure why, I've never liked using explode(), implode(), etc.</p> <p>but I've tried it out, and something went wrong.</p> <pre><code> public function fetch($table, array $criteria = null) { // The query base $query = "SELECT * FROM $table"; // Start checking if ($criteria) { $query .= ' WHERE ' . implode(' AND ', array_map(function($column) { return "$column = ?"; }, array_keys($criteria))); } $check = $this-&gt;pdo-&gt;prepare($query) or die('An error has occurred with the following message:' . $query); $check-&gt;execute(array_values($criteria)); $fetch = $check-&gt;fetch(PDO::FETCH_ASSOC); return $fetch; } </code></pre> <p>This is my query.</p> <p>Basically I will return the variable <code>$fetch</code> which holds the fetch method.</p> <p>and then somewhere, where I want to use the while loop to fetch data, I will use that:</p> <pre><code>$r = new Database(); while ($row = $r-&gt;fetch("argonite_servers", array("server_map" =&gt; "Wilderness"))) { echo $row['server_map']; } </code></pre> <p>Now, I am not getting any errors, but the browser is loading and loading forever, and eventually will get stuck due to lack of memory.</p> <p>That's because the loop is running and running without stopping. Why is it doing this? How can I get this dynamic query to work?</p> <p>EDIT:</p> <pre><code>$r = new Database(); $q = $r-&gt;fetch("argonite_servers", array("server_map" =&gt; "Wilderness")); while ($row = $q-&gt;fetch(PDO::FETCH_ASSOC)) { echo $row['server_map']; } </code></pre>
    singulars
    1. This table or related slice is empty.
    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. 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