[英]HTML tables form MYSQL with PHP while using $WPDB
Hi I am trying to create a table with PHP inside a wordpress shortcode while using the $wpdb connection to the MYSQL table.嗨,我正在尝试在使用 $wpdb 连接到 MYSQL 表的同时,在 wordpress 短代码中使用 PHP 创建一个表。 I have written up /found how to do this with the basic PHP and traditional connection but what I have coded isnt working I am sure I have missed something.我已经写了/找到了如何使用基本的 PHP 和传统连接来做到这一点,但是我编写的代码不起作用我确定我错过了一些东西。 Can someone please tell me in the code below where I have gone wrong?有人可以在下面的代码中告诉我我哪里出错了吗? Thanks.谢谢。
global $wpdb;全球 $wpdb;
$wpdb->query( $wpdb->prepare("SELECT * FROM wp_wpdatatable_4"));
$result=mysql_query(query);
$numfields = mysql_num_fields($result);
echo "<table border=1><tr>";
for ($i=0; $i < $numfields; $i++) // Header
{ echo '<th>'.mysql_field_name($result, $i).'</th>'; }
echo "</tr>";
while
($res=mysql_fetch_row($result)) //you may use mysql_fatch_array($result) which can hendel both $res[name] and $res[1]
{
echo"
<tr>
<td> $res[0]</td>
<td> $res[1]</td>
<td> $res[2]</td>
.
.
.
</tr>";
}
echo"</table>";
}
Your code uses a combination of $wpdb & php mysql functions.您的代码使用了 $wpdb 和 php mysql 函数的组合。
Maybe just use $wpdb也许只是使用 $wpdb
global $wpdb;
$data_table_4 = $wpdb->get_results( "SELECT * FROM wp_wpdatatable_4" );
if ( ! empty( $data_table_4 ) ) {
echo '<table>';
if ( ! empty( $data_table_4 ) ) {
foreach ( $data_table_4 as $data_table_4_row ) {
echo '<tr>';
foreach ( $data_table_4_row as $row_item ) {
echo '<td>' . $row_item . '</td>';
}
echo '</tr>';
}
}
echo '</table>';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.