[英]How to get the columns names along with resultset in php/mysql?
Is this OK ?这个可以吗 ?
$i = 0;
while ($row = mysql_fetch_array($result))
{
$resultset[] = $row;
$columns[] = mysql_fetch_field($result, $i);
}
Then when trying to print然后在尝试打印时
<tr><th><?php echo $columns[0] ?></th><th><?php echo $columns[1] ?></th></tr>
I got an error我有一个错误
Catchable fatal error: Object of class stdClass could not be converted to string
Try the mysql_fetch_field function.试试mysql_fetch_field函数。
For example:例如:
<?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>";
?>
Use mysql_fetch_assoc
to get only an associative array and retrieve the column names with the first iteration:使用
mysql_fetch_assoc
仅获取关联数组并在第一次迭代时检索列名:
$columns = array();
$resultset = array();
while ($row = mysql_fetch_assoc($result)) {
if (empty($columns)) {
$columns = array_keys($row);
}
$resultset[] = $row;
}
Now you can print the head of your table with the first iteration as well:现在,您也可以在第一次迭代中打印表头:
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>';
Here is a more modern solution with ...i
:这是一个更现代的解决方案
...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
而不是返回字符串来避免使用对象。
Try this 尝试这个
$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>";
OR 要么
$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.