php 使用 wordpress $wpdb 在 <table> 中显示数据库中的数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18824461/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Display data from database inside <table> using wordpress $wpdb
提问by bobbyjones
Can someone help me, how to code the logic to print the data from my database into a <table>
?
有人可以帮助我,如何编写将数据从我的数据库打印到<table>
.
<table border="1">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Points</th>
</tr>
<tr>
<?php
global $wpdb;
$result = $wpdb->get_results ( "SELECT * FROM myTable" );
foreach ( $result as $print ) {
echo '<td>' $print->firstname.'</td>';
}
?>
</tr>
</table>
i know this is so basic, but im really having a hard time to make this work.
我知道这是非常基本的,但我真的很难做到这一点。
回答by DS9
Try this:
尝试这个:
<table border="1">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Points</th>
</tr>
<?php
global $wpdb;
$result = $wpdb->get_results ( "SELECT * FROM myTable" );
foreach ( $result as $print ) {
?>
<tr>
<td><?php echo $print->firstname;?></td>
</tr>
<?php }
?>
回答by Charaf JRA
You just need to put <tr>
inside your foreach loop ,and add .
concatenation operator in your line ,you nee alsotry this :
你只需要放入<tr>
你的 foreach 循环中,并.
在你的行中添加连接运算符,你也可以试试这个:
- You need to wrap
<td></td>
inside<tr></tr>
in foreach loop - You need to add
.
concatenation operator in line that contains firstname variable. If you have duplicate values , then add this parameter
ARRAY_A
to your query$result = $wpdb->get_results ( "SELECT * FROM myTable",ARRAY_A );
.<table border="1"> <tr> <th>Firstname</th> <th>Lastname</th> <th>Points</th> </tr> <?php global $wpdb; $result = $wpdb->get_results ( "SELECT * FROM myTable" ); foreach ( $result as $print ) { echo '<tr>'; echo '<td>' . $print->firstname .'</td>'; echo '<td>' . $print->lastname .'</td>'; echo '<td>' . $print->points .'</td>'; echo '</tr>'; } ?> </table>
- 你需要用
<td></td>
里面<tr></tr>
的foreach循环 - 您需要
.
在包含 firstname 变量的行中添加连接运算符。 如果您有重复的值,则将此参数添加
ARRAY_A
到您的查询中$result = $wpdb->get_results ( "SELECT * FROM myTable",ARRAY_A );
.<table border="1"> <tr> <th>Firstname</th> <th>Lastname</th> <th>Points</th> </tr> <?php global $wpdb; $result = $wpdb->get_results ( "SELECT * FROM myTable" ); foreach ( $result as $print ) { echo '<tr>'; echo '<td>' . $print->firstname .'</td>'; echo '<td>' . $print->lastname .'</td>'; echo '<td>' . $print->points .'</td>'; echo '</tr>'; } ?> </table>
回答by Drixson Ose?a
You're missing .
in echo '<td>' $print->firstname.'</td>';
你失踪.
了echo '<td>' $print->firstname.'</td>';
Try this
尝试这个
<?php
global $wpdb;
$result = $wpdb->get_results ( "SELECT * FROM myTable" );
foreach ( $result as $print ) {
echo '<tr>';
echo '<td>' . $print->firstname.'</td>';
echo '<td>' . $print->lastname.'</td>';
echo '<td>' . $print->points.'</td>';
echo '</tr>';
}
?>
回答by Bud Damyanov
Try this:
尝试这个:
$result = $wpdb->get_results("SELECT * FROM myTable" , ARRAY_A); //get result as associative array
Then the usual cycle:
然后是通常的循环:
//spare memory
$count = count($result);
//fastest way to perform the cycle
for ($i = $count; $i--;) {
echo '<td>'. $print[$i]['firstname'].'</td>';
}