$wpdb not work in another page

i create simple plugin , it work in main page , but when i create a link to user go to the another page for see informaation , it show me Fatal error: Uncaught Error: Call to a member function get_results()

my code is :

<?php

global $wpdb;
$results = $wpdb->get_results("SELECT * FROM wp_customers");
?>
<body>
<table>
    <tr>
        <th>‌ID</th>
        <th>Name</th>
        <th>Family</th>
        <th>Numbers</th>
        <th>Tell</th>
    </tr>

    <?php foreach($results as $results){
    ?>
    <tr>
        <td>
            <?php echo $results->id; ?>
        </td>
        <td>
            <?php echo $results->name; ?>
        </td>
        <td>
            <?php echo $results->family; ?>
        </td>
        <td>
            <?php echo $results->numbers; ?>
        </td>
        <td>
            <?php echo $results->tell; ?>
        </td>
        <?php }?>
    </tr>
</table>

Related posts

2 comments

  1. You need to check you have not set up the database prefix to something other than ‘wp_’.

    And if you have wp_ prefix in the database then you have to included wp-load file on the custom page.

    require( '/path/to/wp-load.php' );
    
  2. If it’s a plugin then you should encapsulate your code in a function and hook this funtion to the wordpress “init” action, so in your plugin file you should have somthing like this :

    function your_function_name_here($some_params){
      return "some results";
    }
    
    add_action('init', 'your_function_name_here');
    

Comments are closed.