• Resolved lucavanon

    (@lucavanon)


    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

    (@peterschulznl)

    Hi @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

    (@lucavanon)

    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.