Title: Can&#8217;t access to a custom table from PHP code
Last modified: March 18, 2021

---

# Can’t access to a custom table from PHP code

 *  Resolved [lucavanon](https://wordpress.org/support/users/lucavanon/)
 * (@lucavanon)
 * [5 years, 3 months ago](https://wordpress.org/support/topic/cant-access-to-a-custom-table-from-php-code/)
 * I created a new table on the wordpress database called my_table, but I cannot
   reach it from PHP code.
 *     ```
       <?php
       $my_records = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->my_table");
       echo "<p>Records count is {$my_records}</p>";
       ?>
       ```
   
 * This is not working, but if I change my_table with another wordpress table like“
   users” for example, the PHP code works.
    What am I doing worng? Is it something
   I have to set to make my custom table visible through the $wpdb? Thanks

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Plugin Author [Passionate Programmer Peter](https://wordpress.org/support/users/peterschulznl/)
 * (@peterschulznl)
 * [5 years, 3 months ago](https://wordpress.org/support/topic/cant-access-to-a-custom-table-from-php-code/#post-14201740)
 * Hi [@lucavanon](https://wordpress.org/support/users/lucavanon/),
 * Please change your code as follows to select rows from my_table:
    `$my_records
   = $wpdb->get_var( "SELECT COUNT(*) FROM my_table");`
 * Just use my_table instead of $wpdb->my_table. $wpdb->users works because it points
   to the WP users table as $wpdb adds the WP prefix.
 * Please let me know if this helps,
    Peter
 *  Thread Starter [lucavanon](https://wordpress.org/support/users/lucavanon/)
 * (@lucavanon)
 * [5 years, 3 months ago](https://wordpress.org/support/topic/cant-access-to-a-custom-table-from-php-code/#post-14201853)
 * Thanks!
    It solved the issue. You’re the best!
 * Luca

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Can’t access to a custom table from PHP code’ is closed to new replies.

 * ![](https://ps.w.org/wp-data-access/assets/icon-256x256.png?rev=3299063)
 * [WP Data Access – App Builder for Tables, Forms, Charts, Maps & Dashboards](https://wordpress.org/plugins/wp-data-access/)
 * [Support Threads](https://wordpress.org/support/plugin/wp-data-access/)
 * [Active Topics](https://wordpress.org/support/plugin/wp-data-access/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/wp-data-access/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/wp-data-access/reviews/)

## Tags

 * [custom](https://wordpress.org/support/topic-tag/custom/)
 * [database](https://wordpress.org/support/topic-tag/database/)
 * [php](https://wordpress.org/support/topic-tag/php/)
 * [wpdb](https://wordpress.org/support/topic-tag/wpdb/)

 * 2 replies
 * 2 participants
 * Last reply from: [lucavanon](https://wordpress.org/support/users/lucavanon/)
 * Last activity: [5 years, 3 months ago](https://wordpress.org/support/topic/cant-access-to-a-custom-table-from-php-code/#post-14201853)
 * Status: resolved