$ wpdb错误无法修复

时间:2016-04-12 11:24:36

标签: wordpress select wpdb

我创建自定义插件,我有表wp_customers,我尝试选择我的表中的所有东西,但wordpress只显示致命错误:未捕获错误:在C:\ xampp \ htdocs中调用null成员函数get_results() \ wordpress \ wordpress \ wp-content \ plugins \ rflredirection \ public \ showResult.php:4堆栈跟踪:#0 {main}抛出C:\ xampp \ htdocs \ wordpress \ wordpress \ wp-content \ plugins \ rflredirection \ public第4行的showResult.php

我看到codex,我使用一些antoher代码进行选择,但我只是那个massege,这是我的代码尝试

global $wpdb;

$results = $wpdb->get_results('SELECT * FROM $wpd->wp_customers DESC ');

$results = $wpdb->get_results('SELECT * FROM wp_customers');

$table_name = $wpdb->prefix . "wp_customers";
$results = $wpdb->get_results("SELECT code FROM ".$table_name");

我尝试获取版本但没有

$dbVersion = $wpdb->db_version();
if ($dbVersion) {
    echo "Running MySQL " . $dbVersion;
} else {
    echo "MySQL version unavailable.";
}

1 个答案:

答案 0 :(得分:1)

使用 wpdb类 时,无需在表名中包含wp_

应该是这样的:

global $wpdb;
$results = $wpdb->get_results("SELECT * FROM $wpdb->customers");
//                                                  ^ use without (wp_)

编辑#1:

foreach循环错误:

您应该写foreach($results as $rows)而不是错误:foreach($rows as $results)

并且在获取值echo $rows["id"];等时不要忘记分号。

您的代码应该像:

<?php
 global $wpdb;
 $results = $wpdb->get_results("SELECT * FROM $wpdb->customers");
?>
<body>
 <table> 
    <tr> 
        <th>‌ID</th>
         <th>Name</th> 
         <th>Family</th>
          <th>Numbers</th> 
          <th>Tell</th> 
      </tr>
       <?php foreach($results as $rows){
        ?> 
        <tr> 
            <td>
                <?php echo $rows["id"]; ?>
            </td> 
            <td>
                <?php echo $rows["name"]; ?>
            </td> 
            <td>
                <?php echo $rows["family"]; ?>
            </td> 
            <td>
                <?php echo $rows["numbers"]; ?>
            </td> 
            <td>
                <?php echo $rows["tell"]; ?>
            </td>
             <?php }?> 
         </tr>
</table>

编辑#2:

复制所有这一行并粘贴到您的页面顶部以包含wordpress包:

<?php
/**
 * WordPress User Page
 *
 * Handles authentication, registering, resetting passwords, forgot password,
 * and other user handling.
 *
 * @package WordPress
 */

// Make sure that the WordPress bootstrap has run before continuing.
require( dirname(__FILE__) . '/wp-load.php' );

// Now your page codes goes here ...