2 回答

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超2個(gè)贊
你打電話(huà)時(shí)...
$aocol = count(mysqli_fetch_array($erg, MYSQLI_NUM));
這實(shí)際上是在讀取第一行,因此它不再可用于以下循環(huán)。
我建議重組您的代碼,以便在主讀取循環(huán)內(nèi)構(gòu)建頭部,但只有當(dāng)$head它為空時(shí)......
//table head
$head = "";
//only if style.css included-->irrelevant
echo '<div class="table"><table id="table">';
//output of table's body --> here must be the bug, I think
while($zeile = mysqli_fetch_array($erg, MYSQLI_NUM)) {
if ( empty ($head) ) {
//counts the amount of columns
$aocol = count($zeile);
for($x = 0; $x < $aocol; $x++) {
//legt alle Informationen des Feldes $x in $finfo, darunter auch den Namen der Spalte.
//puts all information of the field $x in $finfo, also the name of the column
$finfo = mysqli_fetch_field_direct($erg, $x);
//Schreibt die Kopfzeile der Tabelle
//writes the table's head
$head .= "<th>".$finfo->name."</th>";
}
//output of table's head
echo "<th>$head</th>";
}
//new tablerow
echo "<tr>";
//filling the row
foreach($zeile as $feld) {
//format for numbers
$ra = preg_match('/^\d+$/',$feld) ? ' align="right"' : '';
//displaying table data
echo "<td$ra>".$feld."</td>";
}
}
此外,如果您更改MYSQLI_NUM為MYSQLI_ASSOC,那么您可以只使用鍵名作為列名并刪除額外的 API 調(diào)用......
while($zeile = mysqli_fetch_array($erg, MYSQLI_ASSOC)) {
if ( empty ($head) ) {
foreach ( $zeile as $name => $value ) {
$head .= "<th>".$name."</th>";
}
//output of table's head
echo "<th>$head</th>";
}

TA貢獻(xiàn)1789條經(jīng)驗(yàn) 獲得超10個(gè)贊
您應(yīng)該使用這樣的查詢(xún)來(lái)獲取所有結(jié)果:
$query = "SELECT * FROM table ORDER BY `id` LIMIT 0, 5";
這只會(huì)給你前 5 個(gè)結(jié)果,然后你可以:
$query = "SELECT * FROM table ORDER BY `id` LIMIT 5, 100";
或這個(gè):
$query = "SELECT * FROM table ORDER BY `id` DESC LIMIT 5";
- 2 回答
- 0 關(guān)注
- 153 瀏覽
添加回答
舉報(bào)