【问题标题】:Wordpress: Fetching Data from Database $wpdb of Specific UserWordpress:从特定用户的数据库 $wpdb 中获取数据
【发布时间】:2020-09-17 06:59:26
【问题描述】:

您好,我今天正在做一个项目。我想在每次登录时在前端打印特定用户的数据。当他们登录时将获得他们的用户 ID,并且只会从该特定用户那里获取数据。

I have this database. I want vendor_id 3 when this vendor login will display only values of specific vendor if no values will return "No Commission Data for this user"

现在这是我的 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>

And it returns the data of this. But what I want is only as per user like right now Im login as vendor id 3. It show all. What is the proper wordpress php conditioning code I need to use

感谢您的回答。

【问题讨论】:

  • 您应该在此处将代码添加为代码块而不是图像。
  • 对不起,我在堆栈中添加了上面的代码,之前没有真正使用它...

标签: php mysql wordpress


【解决方案1】:

您的代码是正确的,但您从数据库中选择数据的条件是错误的

当你不应用 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>

How to use where clause in MySQL

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-07
    • 2020-09-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多