我有一个表从MySQL数据库正在制定4个领域:id
,postcode
,blacklist_reason
,signup_attempts
。最后一个MySQL结果数组的行闯入怪阵
然后我有以下类输出和处理这个,$MySQL
是一个MySQL类,ExecuteSQL
返回一个数组,而不是MySQL资源。如预期
class Blacklist {
private $MySQL = null;
private $rowsPerPage = 6;
public function __construct($MySQL) {
$this->MySQL = $MySQL;
}
public function displayBlacklistList($page = 1) {
($page == 1) ? $start = 0 : $start = $this->rowsPerPage*($page-1);
$finish = $this->rowsPerPage;
$sql = "SELECT * FROM blacklist LIMIT $start, $finish";
$res = $this->MySQL->ExecuteSQL($sql);
echo '
<p><a href="#" class="btn btn-success">Add row</a></p>
<table class="table table-hover table-bordered table-striped">
<thead>
<tr>
<th>ID</th>
<th>Postcode</th>
<th>Blacklist reason</th>
<th>Signup attempts</th>
<th>Actions</th>
</tr>
</thead>
<tbody>';
foreach($res as $k => $v) {
echo '
<tr>
<td>'.$v['id'].'</td>
<td>'.$v['postcode'].'</td>
<td>'.$v['blacklist_reason'].'</td>
<td>'.$v['signup_attempts'].'</td>
<td><a href="#" class="btn btn-primary">Edit</a> <a href="#" class="btn btn-danger">Remove</a></td>
</tr>';
}
echo '
</tbody>
</table>';
echo $this->_displayPagination($page, $res);
}
}
一切正常,并显示除非我有,例如,6行中的DB,并设置$this->rowsPerPage = 5
,因为这将导致第二页(显示最后一个行)中循环,并显示为有尽可能多的行字段,每个单元格中的数据是预期结果的第一个字符。如果我在表10行显示9每页,同样的情况,等
例如,我得到这个:
ID Postcode Blacklist reason Signup attempts
7 7 7 7
B B B B
A A A A
6 6 6 6
当我期望:
ID Postcode Blacklist reason Signup attempts
7 BH233SF A reason 6
的问题显然与foreach()
循环,我习惯于在这里使用while(mysql_fetch_assoc())
循环,但在这个例子中我使用返回数组的类,而不是对象,我不知道为什么会发生这种情况。
- 问题的答案 -
executeSQL
返回关联数组:
Array ([id] => 7 [postcode] => BH233SF [blacklist_reason] => A reason [signup_attempts] => 6)
什么它返回是好的,为的var_dump也证明:
array(4) { ["id"]=> string(1) "7" ["postcode"]=> string(7) "BH233SF" ["blacklist_reason"]=> string(8) "A reason" ["signup_attempts"]=> string(1) "6" }
它显示了..你试过'的var_dump($水库)'每列的第一个字符? – BlitZ
ExecuteSQL方法返回什么? – bwoebi
增加了额外的信息到问题的底部。谢谢。 – artparks