首页 > 解决方案 > wordpress 获取 mysql 表

问题描述

我使用 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 }
?>              

标签: phpmysqlwordpresstemplates

解决方案


检查这个伪代码

在您的模板文件中,您可以使用。

global $wpdb;    
$result = $wpdb->get_results ( "SELECT * FROM sample_table" );    
foreach ( $result as $print )   { 
echo $print->message;
}

推荐阅读