php - Wordpress:从特定用户的数据库 $wpdb 中获取数据
问题描述
你好我今天正在做一个项目。我想在每次登录时在前端打印特定用户的数据。当他们登录时,将获取他们的用户 ID,并仅从该特定用户那里获取数据。
我有这个数据库。我想要 vendor_id 3 当此供应商登录将仅显示特定供应商的值时,如果没有值将返回“此用户无佣金数据”
现在这是我的 wordpress wpdb 代码
<table border="1">
<tr>
<th>Vendor ID</th>
<th>Total Due</th>
<th>Time</th>
</tr>
<?php
global $wpdb;
$result = $wpdb->get_results ( "SELECT * FROM wp_pv_commission" );
$vendor = get_current_user_id();
foreach ( $result as $userdata) {
?>
<tr>
<td><?php echo $userdata->vendor_id;?></td>
<td><?php echo $userdata->total_due;?></td>
<td><?php echo $userdata->time;?></td>
</tr>
<?php
}
?>
</table>
并返回 this 的数据。但我想要的只是像现在这样的用户,我以供应商 ID 3 登录。它显示了所有内容。我需要使用的正确的 wordpress php 调节代码是什么
谢谢您的回答。
解决方案
您的代码是正确的,但您从数据库中选择数据的条件是错误的
当您不应用 where 子句时,MYSQL 将始终返回所有记录。因此,您需要在查询中提供 where 子句
<table border="1">
<tr>
<th>Vendor ID</th>
<th>Total Due</th>
<th>Time</th>
</tr>
<?php
global $wpdb;
$vendor = get_current_user_id();
$result = $wpdb->get_results ( "SELECT * FROM wp_pv_commission where vendor_id=".$vendor );
foreach ( $result as $userdata) {
?>
<tr>
<td><?php echo $userdata->vendor_id;?></td>
<td><?php echo $userdata->total_due;?></td>
<td><?php echo $userdata->time;?></td>
</tr>
<?php
}
?>
</table>
推荐阅读
- c# - 将组合框弹出边框连接到主组合框边框
- python - 简化歌曲和艺术家姓名
- python - 在两个单词之间插入连字符以在 Python 中的 URL 中使用
- group-by - 将列数据分组并存储在 Mysql 中的单独命名列中
- spring-boot - spring-boot-maven-plugin build-info.properties
- google-developers-console - 您的付款资料目前已暂停 - Google 控制台
- r - 如何在传单地图中为形状文件的部分着色
- python - Dask 数据框:`set_index` 可以将单个索引放入多个分区吗?
- javascript - Javascript 生成的输入被忽略
- javascript - React Native:使用有效负载导航的操作未由任何导航器处理