[英]How to get the columns names along with resultset in php/mysql?
這個可以嗎 ?
$i = 0;
while ($row = mysql_fetch_array($result))
{
$resultset[] = $row;
$columns[] = mysql_fetch_field($result, $i);
}
然后在嘗試打印時
<tr><th><?php echo $columns[0] ?></th><th><?php echo $columns[1] ?></th></tr>
我有一個錯誤
Catchable fatal error: Object of class stdClass could not be converted to string
試試mysql_fetch_field函數。
例如:
<?php
$dbLink = mysql_connect('localhost', 'usr', 'pwd');
mysql_select_db('test', $dbLink);
$sql = "SELECT * FROM cartable";
$result = mysql_query($sql) or die(mysql_error());
// Print the column names as the headers of a table
echo "<table><tr>";
for($i = 0; $i < mysql_num_fields($result); $i++) {
$field_info = mysql_fetch_field($result, $i);
echo "<th>{$field_info->name}</th>";
}
// Print the data
while($row = mysql_fetch_row($result)) {
echo "<tr>";
foreach($row as $_column) {
echo "<td>{$_column}</td>";
}
echo "</tr>";
}
echo "</table>";
?>
使用mysql_fetch_assoc
僅獲取關聯數組並在第一次迭代時檢索列名:
$columns = array();
$resultset = array();
while ($row = mysql_fetch_assoc($result)) {
if (empty($columns)) {
$columns = array_keys($row);
}
$resultset[] = $row;
}
現在,您也可以在第一次迭代中打印表頭:
echo '<table>';
$columns = array();
$resultset = array();
while ($row = mysql_fetch_assoc($result)) {
if (empty($columns)) {
$columns = array_keys($row);
echo '<tr><th>'.implode('</th><th>', $columns).'</th></tr>';
}
$resultset[] = $row;
echo '<tr><td>'.implode('</td><td>', $rows).'</td></tr>';
}
echo '</table>';
這是一個更現代的解決方案...i
:
$db_link = @mysqli_connect($server, $user, $pass, $db) or die("Connection failed");
$spalten = ['pc_name', 'pc_type', 'pc_snr', 'pc_bj', 'mo_port_1', 'mo_snr_1', 'mo_bj_1', 'mo_port_2', 'mo_snr_2', 'mo_bj_2'];
$abfrage = "SELECT " . implode(',', $spalten) . " FROM hardware order by pc_name";
$liste = mysqli_query($db_link, $abfrage);
$spalten = mysqli_num_fields($liste);
while ($werte[] = mysqli_fetch_array($liste, MYSQLI_ASSOC)) {}
<table class="display" id="table" style="width:100%">
<thead>
<tr>
<?php
for ($i = 0; $i < $spalten; $i++) {
$feldname[$i] = mysqli_fetch_field_direct($liste, $i)->name;
echo '<th>' . ucfirst($feldname[$i]) . '</th>';
}
?>
</tr>
</thead>
<tbody>
<?php
for ($i = 0; $i < mysqli_num_rows($liste); $i++) {
?>
<tr>
<?php for ($j = 0; $j < $spalten; $j++) {
?>
<td><?php
echo $werte[$i][$feldname[$j]];
?>
</td>
<?php } ?>
</tr>
<?php } ?>
</tbody>
</table>
盡管它已被棄用並且不再在 PHP 7 中使用,但您可以通過使用函數mysql_field_name
而不是返回字符串來避免使用對象。
嘗試這個
$result=mysql_query('SELECT * FROM `table1` where id=5') or die ('query failed');
echo "<table>";
while ($row=mysql_fetch_assoc($result)) {
$column = array_keys($row);
for($i=0; $i<sizeof($column); $i++){
echo "<tr><th>".$column[$i]."</th><td>".$row[$column[$i]]."</td></tr>";
}
}
echo "</table>";
要么
$result=mysql_query('SELECT * FROM `table1` where id=5') or die ('query failed');
echo "<table>";
$row=mysql_fetch_assoc($result);
$column = array_keys($row);
for($i=0; $i<sizeof($column); $i++){
echo "<tr><th>".$column[$i]."</th><td>".$row[$column[$i]]."</td></tr>";
}
echo "</table>";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.