我有一個表從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