我使用 WordPress 作为我网站的基础,我创建了一个自定义(不是 wp 默认)MySQL 表,其中包含 3 列,我想在其中存储简单信息,例如姓名、生日和电子邮件联系人。不过,我还想在我的 WordPress 站点中创建一个可以连接到这个特定数据库的页面。我想显示这个表数据,如 WordPress 帖子模板。有简单的方法吗?
<?php /* Template Name: CustomPageT1 */ ?>
<h1>This is my custom page</h1>
<table border="1">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Points</th>
</tr>
<?php
global $wpdb;
$result = $wpdb->get_results ( "SELECT * FROM sample_table" );
foreach ( $result as $print ) {
?>
<tr>
<td><?php echo $print->message;?></td>
</tr>
<?php }
?>
最佳答案
您可以使用 wpdb 类来实现此目的。
$new_connection = new wpdb('user','pass','database_name','localhost');
$result = $new_connection->get_results("select * from my_table");
foreach ( $result as $print ) {
?>
<tr>
<td><?php echo $print->message;?></td>
</tr>
<?php }
关于php - wordpress获取mysql表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51222100/