title: Working with multiple rows from a MySQL query
body: <p>Before I begin, I want to point out that I can solve my problem. I've rehearsed enough in PHP to be able to get a workaround to what I'm trying to do. <em>However</em> I want to make it modular; without going too much into detail to further confuse my problem, I will simplify what I am trying to do so that way it does not detract from the purpose of what I'm doing. Keep that in mind.</p>
  
  <p>I am developing a simple CMS to manage a user database and edit their information. It features pagination (which works), and a button to the left that you click to open up a form to edit their information and submit it to the database (which also works).</p>
  
  <p>What does not work is displaying each row from MySQL in a table using a very basic script which I won't get into too much detail on how it works. But it basically does a database query with this:</p>
  
  <pre><code>SELECT * FROM users OFFSET (insert offset here) LIMIT (insert limit here)
  </code></pre>
  
  <p>Essentially, with pagination, it tells what number to offset, and the limit is how many users to display per page. These are set, defined, and tested to be accurate and <em>they do work.</em> However, I am not too familiar how to handle these results.</p>
  
  <p>Here is an example query on page 2 for this CMS:</p>
  
  <pre><code>SELECT * FROM users OFFSET 10 LIMIT 10
  </code></pre>
  
  <p>This should return 10 rows, 10 users down in the database. And it does, when I try this command in command prompt, it gives me what I need:
  <img src="https://i.stack.imgur.com/MVCo4.png" alt="enter image description here"></p>
  
  <p>But when I try to handle this data in PHP like this:</p>
  
  <pre><code>&lt;?php
      while ($row = $db-&gt;query($pagination-&gt;get_content(), "row")) {
          print_r($row);
  
      }
  ?&gt;
  </code></pre>
  
  <p>$db->query method is:</p>
  
  <pre><code>public function query($sql, $type = "assoc") {
      $this-&gt;last_query = $sql;
      $result = mysql_query($sql, $this-&gt;connection);
      $this-&gt;confirm_query($result);
  
      if ($type == "row") {
          return mysql_fetch_row($result);
      } elseif ($type == "assoc" || true) {
          return mysql_fetch_assoc($result);
      } elseif ($type == "array") {
          return mysql_fetch_array($result);
      } elseif ($type == false) {
          return $result;
      }
  }
  </code></pre>
  
  <p>$pagination->get_content method is:</p>
  
  <pre><code>    public function get_content() {
          global $db;
  
          $query = $this-&gt;base_sql;
  
          if (!isset($_GET["page"])) {
              $query .= " LIMIT {$this-&gt;default_limit}";
              return $query;
          } elseif (isset($_GET["page"]) &amp;&amp; $_GET["page"] == 1) {
              $query .= " LIMIT {$this-&gt;default_limit}";
              return $query;
          } elseif (isset($_GET["page"])) {
              $query .= " LIMIT {$this-&gt;default_limit}";
              $query .= " OFFSET " . (($_GET["page"] * $this-&gt;default_limit) - 10);
              return $query;
          }
      }
  </code></pre>
  
  <p>And my results from the while loop (which should print out each row of the database, no?) gives me the same row everytime, continuously until PHP hits the memory limit/timeout limit.</p>
  
  <p><img src="https://i.stack.imgur.com/ZAmMv.png" alt="enter image description here"></p>
  
  <p>Forgive me if its something simple. I rarely ever handle database data in this manner. What I want it to do is show the 10 users I requested. Feel free to ask any questions.</p>
  
  <p><strong>AFTER SOME COMMENTS, I'VE DECIDED TO SWITCH TO MYSQLI FUNCTIONS AND IT WORKS</strong></p>
  
  <pre><code>    // performs a query, does a number of actions dependant on $type
  public function query($sql, $type = false) {
      $sql = $this-&gt;escape($sql);
  
      if ($result = $this-&gt;db-&gt;query($sql)) {
          if ($type == false) {
              return $result;
          } elseif ($type == true || "assoc") {
              if ($result-&gt;num_rows &gt;= 2) {
                  $array;
                  $i = 1;
  
                  while ($row = $result-&gt;fetch_assoc()) {
                      $array[$i] = $row;
                      $i++;
                  }
  
                  return $array;
              } elseif ($result-&gt;num_rows == 1) {
                  return $result-&gt;fetch_assoc();
              }
          } elseif ($type == "array") {
              if ($result-&gt;num_rows &gt;= 2) {
                  $array;
                  $i = 1;
  
                  while ($row = $result-&gt;fetch_array()) {
                      $array[$i] = $row;
                      $i++;
                  }
  
                  return $array;
              } elseif ($result-&gt;num_rows == 1) {
                  return $result-&gt;fetch_array();
              }
          }
      } else {
          die("There was an error running the query, throwing error: " . $this-&gt;db-&gt;error);
      }
  }
  </code></pre>
  
  <p>Basically, in short, I took my entire database, deleted it, and remade another one based on the OOD mysqli (using the class mysqli) and reformatted it into a class that extends mysqli. A better look at the full script can be found here:</p>
  
  <p><a href="http://pastebin.com/Bc00hESn" rel="nofollow noreferrer">http://pastebin.com/Bc00hESn</a></p>
  
  <p>And yes, it does what I want it to. It queries multiple rows, and I can handle them however I wish using the very same methods I planned to do them in. Thank you for the help.</p>