Php 正在尝试从wp DB检索数据

Php 正在尝试从wp DB检索数据,php,wordpress,Php,Wordpress,我正在尝试从wp数据库中检索数据,下面是我的插件代码。我已经在数据库中手动添加了记录,但是我的插件不想检索我的数据。我不知道我哪里出了问题 <?php /** * Plugin Name: Member Details */ function custom_view() { global $wpdb; echo '<table> <tr> &

我正在尝试从wp数据库中检索数据,下面是我的插件代码。我已经在数据库中手动添加了记录,但是我的插件不想检索我的数据。我不知道我哪里出了问题

<?php
    /**
     * Plugin Name: Member Details         
     */

    function custom_view() {
     global $wpdb;          

     echo '<table>
      <tr>
        <th>Name</th>
        <th>Email</th>
        <th>Bank</th>
        <th>Account Number</th>
        <th>Deposited Amount</th>
        <th>Deposit Date</th>
        <th>Period [Days]</th>            
      </tr>';
     $results = $wpdb->get_results("select * from users"); 

     foreach( $results as $user_data) {                  
      // $roi = $user_data->amount * $user_data->period;  
      // $amount_growth = $roi - $user_data->amount;

       echo "<tr>
        <td>$user_data->user_nicename</td>
        <td>$user_data->user_email</td>
        <td>$user_data->bank</td>
        <td>$user_data->account_num</td>
        <td>$user_data->amount</td>
        <td>$user_data->deposit_date</td>
        <td>$user_data->period</td>
        <td></td>
        <td></td>
      </tr>";
     }
     echo '</table>';         
    }
    add_shortcode('views', 'custom_view');
    ?>

有人能帮我吗?谢谢

两件重要的事情:

您的数据没有返回,因为您没有传递表格前缀,即表格名称错误。 示例“users”是正确的“wp_users”或在数据库中创建时使用的前缀。 您可以使用$wpdb->users变量,该变量将自动返回您在wp中注册的前缀

短代码必须始终以返回结束,而不是以回波结束。 我给您留下了一个示例,如果您想使用php,可以手动调用它。但您可以直接在编辑器中使用它,通常只需调用shortcode:)

如果我的回答对你有帮助。请投票并将其作为正确答案结束此问题:)


改进了格式,删除了一些不必要的注释和行。请将答案标记为正确以结束问题。谢谢你的投票,我很高兴能帮助你D
<?php 

    /**
 * Plugin Name:       Member Details
 * Plugin URI:        https://mysite.co.za
 * Description:       All Member Details List 
 * Version:           1.0
 * Author:            Empire Investment
 * Author URI:        https://mysite.co.za
 */

function custom_view() {
 global $wpdb;


 $output .= '<table>
  <tr>
    <th>Name</th>
    <th>Email</th>
    <th>Bank</th>
    <th>Account Number</th>
    <th>Deposited Amount</th>
    <th>Deposit Date</th>
    <th>Period [Days]</th>

  </tr>';
 $results = $wpdb->get_results("select * from $wpdb->users"); 

 foreach( $results as $user_data) {


// $roi = $user_data->amount * $user_data->period;  
// $amount_growth = $roi - $user_data->amount;


 $output .= "<tr>
    <td>$user_data->user_nicename</td>
    <td>$user_data->user_email</td>
    <td>$user_data->bank</td>
    <td>$user_data->account_num</td>
    <td>$user_data->amount</td>
    <td>$user_data->deposit_date</td>
    <td>$user_data->period</td>
    <td></td>
    <td></td>
  </tr>";
 }
 $output .= '</table>';

 return $output;
}
add_shortcode('views', 'custom_view');

//example usae with php
echo do_shortcode( $content, '[custom_view]' );