我的查询从我的表中选择所有列
$spool = $wpdb->get_results('SELECT * FROM `tablename`');
然后我将结果显示在表格中。我需要显示所有列。我就是这样做的。
echo "<table>";
if ( !empty( $spool ) ) {
echo "<tr><th> header </th></tr>";
foreach ( $spool as $a ) {
echo "<tr><th>" . $a->columnname1 . "</th><th>" .$a->columnnameN . "</th></tr>";
}
}
echo "</table>";
现在我有大约 40 个列,我想问一下是否有更智能、更简单的显示它们的方式。
最佳答案
可能你需要嵌套循环,你得到的结果可能是这样的
array(
0:{column1:test,column2:test . . . },
1:{column1:test,column2:test . . . }
)
所以你可以这样试试
echo "<table>";
if ( !empty( $spool ) ) {
echo "<tr><th> header </th></tr>";
foreach ( $spool as $key => $value ) {
echo '<tr>';
foreach ( $value as $a ) {
echo "<th>" . $a. "</th>";
}
echo '</tr>';
}
}
echo "</table>";
关于php - 使用php从sql表中提取列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46222713/