php - 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 }
?>
解决方案
检查这个伪代码
在您的模板文件中,您可以使用。
global $wpdb;
$result = $wpdb->get_results ( "SELECT * FROM sample_table" );
foreach ( $result as $print ) {
echo $print->message;
}
推荐阅读
- c++ - QImage.save 太慢了
- c# - 策略模式和两个存储库
- android - 无法使用普通 setImageUri 和 Picasso 在 ImageView 上设置图像 - Android Nougat
- qt - 构建 libQGLViewer 错误:找不到 lqglviewer-qt5
- regex - 使用正则表达式从浮点数中去除尾随零
- asp.net - 为什么在使用 POST 重定向到页面时必须使用表单
- node.js - 我的本地主机登录页面上的 POST 请求给出了 ERR_EMPTY_RESPONSE
- ios - AccessibilityTraits StartsMedia 不起作用
- elasticsearch-dsl - 尝试在 elasticsearch-dsl.py 持久性 api 中使用日期时出现异常
- scala - 带有幻影的单个事务中的多个语句