php - wordpress获取mysql表

标签 php mysql wordpress templates

我使用 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/

相关文章:

javascript - 每次回复后提交 RSVP 表格 - 需要为家人一起提交所有回复

javascript - google maps和mysql数据库radius search中存储的地址类似

php - WordPress PHP 电子邮件脚本

php - 当数据库中找不到值时如何中断循环

php - 将 1 美元除以 3 并调整 1 美分

javascript - 拉维尔 5 : Updating Multiple Records

mysql - CakePHP 模型在 SQL 语句中使用了错误的字段!

需要 PHP 用户登录、数据库身份验证和重定向帮助

javascript - 在 wordpress 中使用 Jquery 隐藏带有图像的文本

php - 如何使用php在html中包含css