Note that there are some explanatory texts on larger screens.

plurals
  1. POLast row of a MySQL result array breaks into strange array
    primarykey
    data
    text
    <p>I have a table being drawn from a MySQL database with 4 fields: <code>id</code>, <code>postcode</code>, <code>blacklist_reason</code>, <code>signup_attempts</code>. </p> <p>I then have the following class to output and deal with this, <code>$MySQL</code> is a mysql class with <code>ExecuteSQL</code> returning an array, not a MySQL resource.</p> <pre><code>class Blacklist { private $MySQL = null; private $rowsPerPage = 6; public function __construct( $MySQL ) { $this-&gt;MySQL = $MySQL; } public function displayBlacklistList( $page = 1 ) { ( $page == 1 ) ? $start = 0 : $start = $this-&gt;rowsPerPage*($page-1); $finish = $this-&gt;rowsPerPage; $sql = "SELECT * FROM blacklist LIMIT $start, $finish"; $res = $this-&gt;MySQL-&gt;ExecuteSQL($sql); echo ' &lt;p&gt;&lt;a href="#" class="btn btn-success"&gt;Add row&lt;/a&gt;&lt;/p&gt; &lt;table class="table table-hover table-bordered table-striped"&gt; &lt;thead&gt; &lt;tr&gt; &lt;th&gt;ID&lt;/th&gt; &lt;th&gt;Postcode&lt;/th&gt; &lt;th&gt;Blacklist reason&lt;/th&gt; &lt;th&gt;Signup attempts&lt;/th&gt; &lt;th&gt;Actions&lt;/th&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody&gt;'; foreach( $res as $k =&gt; $v ) { echo ' &lt;tr&gt; &lt;td&gt;'.$v['id'].'&lt;/td&gt; &lt;td&gt;'.$v['postcode'].'&lt;/td&gt; &lt;td&gt;'.$v['blacklist_reason'].'&lt;/td&gt; &lt;td&gt;'.$v['signup_attempts'].'&lt;/td&gt; &lt;td&gt;&lt;a href="#" class="btn btn-primary"&gt;Edit&lt;/a&gt; &lt;a href="#" class="btn btn-danger"&gt;Remove&lt;/a&gt;&lt;/td&gt; &lt;/tr&gt;'; } echo ' &lt;/tbody&gt; &lt;/table&gt;'; echo $this-&gt;_displayPagination( $page, $res ); } } </code></pre> <p>Everything works and displays as expected unless I have, for example, 6 rows in the DB and set <code>$this-&gt;rowsPerPage = 5</code> as this causes the second page (showing one final row) to loop and show as many rows as there are fields in the table, with the data in each cell being the first character of the expected result. If I have 10 rows in the table and show 9 per page, the same happens, etc.</p> <p>For example I get this:</p> <pre><code>ID Postcode Blacklist reason Signup attempts 7 7 7 7 B B B B A A A A 6 6 6 6 </code></pre> <p>When I would expect:</p> <pre><code>ID Postcode Blacklist reason Signup attempts 7 BH233SF A reason 6 </code></pre> <p>The issue is obviously with the <code>foreach()</code> loop, I'm used to using a <code>while( mysql_fetch_assoc() )</code> loop here but in this instance am using a class that returns arrays, not objects and I can't figure out why this is happening.</p> <p>-- ANSWERS TO QUESTIONS --</p> <p><code>executeSQL</code> returns an associative array:</p> <pre><code>Array ( [id] =&gt; 7 [postcode] =&gt; BH233SF [blacklist_reason] =&gt; A reason [signup_attempts] =&gt; 6 ) </code></pre> <p>What it is returning is fine, as var_dump also proves:</p> <pre><code>array(4) { ["id"]=&gt; string(1) "7" ["postcode"]=&gt; string(7) "BH233SF" ["blacklist_reason"]=&gt; string(8) "A reason" ["signup_attempts"]=&gt; string(1) "6" } </code></pre>
    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. 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