Access custom table using global wpdb not working

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Access custom table using global wpdb not working

ChrisHe
Hi,
Just recently a custom plugin on my site stopped working properly (after
several years).
The following line that fails is simply getting a count from a custom
(non-wp) table.

global $wpdb;
...
$num = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->custom_tablename");
// result is null because it appears that $wpdb->custom_tablename is
also null.

$num_wpUsers = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->users"); //
this works as expected!

If there some change that only allows "standard" wordpress table names now?

Thanks for any help!
Chris


---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus
_______________________________________________
wp-hackers mailing list
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers
Reply | Threaded
Open this post in threaded view
|

Re: Access custom table using global wpdb not working

David F. Carr-2
As far as I know, custom table names are not automatically populated in the
$wpdb object (never worked when I tried it). I usually do something like
$custom_tablename = $wpdb->prefix.'custom_tablename' and then use that
variable in my sql.

You're saying this used to work. Is it a plugin of your design? Could there
have been something in the plugin code (now changed or deleted) that added
an additional property to the $wpdb object?


On Tue, Dec 27, 2016 at 1:17 PM, Chris Hearn <[hidden email]>
wrote:

> Hi,
> Just recently a custom plugin on my site stopped working properly (after
> several years).
> The following line that fails is simply getting a count from a custom
> (non-wp) table.
>
> global $wpdb;
> ...
> $num = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->custom_tablename"); //
> result is null because it appears that $wpdb->custom_tablename is also null.
>
> $num_wpUsers = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->users"); //
> this works as expected!
>
> If there some change that only allows "standard" wordpress table names now?
>
> Thanks for any help!
> Chris
>
>
> ---
> This email has been checked for viruses by Avast antivirus software.
> https://www.avast.com/antivirus
> _______________________________________________
> wp-hackers mailing list
> [hidden email]
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>
_______________________________________________
wp-hackers mailing list
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers
Reply | Threaded
Open this post in threaded view
|

Re: Access custom table using global wpdb not working

ChrisHe
HI David,

I am changing my code to do as you suggested. But yes, I am saying that
the $wpdb->custom_tablename used to work!

I am going through the code, because its a long time since I wrote it!

Thanks for your reply

Chris


On 27/12/2016 18:27, David F. Carr wrote:

> As far as I know, custom table names are not automatically populated in the
> $wpdb object (never worked when I tried it). I usually do something like
> $custom_tablename = $wpdb->prefix.'custom_tablename' and then use that
> variable in my sql.
>
> You're saying this used to work. Is it a plugin of your design? Could there
> have been something in the plugin code (now changed or deleted) that added
> an additional property to the $wpdb object?
>
>
> On Tue, Dec 27, 2016 at 1:17 PM, Chris Hearn <[hidden email]>
> wrote:
>
>> Hi,
>> Just recently a custom plugin on my site stopped working properly (after
>> several years).
>> The following line that fails is simply getting a count from a custom
>> (non-wp) table.
>>
>> global $wpdb;
>> ...
>> $num = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->custom_tablename"); //
>> result is null because it appears that $wpdb->custom_tablename is also null.
>>
>> $num_wpUsers = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->users"); //
>> this works as expected!
>>
>> If there some change that only allows "standard" wordpress table names now?
>>
>> Thanks for any help!
>> Chris
>>
>>
>> ---
>> This email has been checked for viruses by Avast antivirus software.
>> https://www.avast.com/antivirus
>> _______________________________________________
>> wp-hackers mailing list
>> [hidden email]
>> http://lists.automattic.com/mailman/listinfo/wp-hackers
>>
> _______________________________________________
> wp-hackers mailing list
> [hidden email]
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>



---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus
_______________________________________________
wp-hackers mailing list
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers
Reply | Threaded
Open this post in threaded view
|

Re: Access custom table using global wpdb not working

Claudio Simeone
In reply to this post by ChrisHe
2016-12-27 19:17 GMT+01:00 Chris Hearn <[hidden email]>:

[cut]

> If there some change that only allows "standard" wordpress table names now?

Hi Chris, to add custom tables in the $wpdb object I did in this way

add_action( 'init', 'register_custom_table', 1 );
add_action( 'switch_blog', 'register_custom_table' ); // for multisite

function register_custom_table() {
    global $wpdb;
    $wpdb->custom_tablename = $wpdb->prefix . 'custom_tablename';
}


regards
Claudio
_______________________________________________
wp-hackers mailing list
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers
Reply | Threaded
Open this post in threaded view
|

Re: Access custom table using global wpdb not working

ChrisHe
Thanks Claudio!

Chris


On 11/01/2017 06:41, Claudio Simeone wrote:

> 2016-12-27 19:17 GMT+01:00 Chris Hearn <[hidden email]>:
>
> [cut]
>
>> If there some change that only allows "standard" wordpress table names now?
> Hi Chris, to add custom tables in the $wpdb object I did in this way
>
> add_action( 'init', 'register_custom_table', 1 );
> add_action( 'switch_blog', 'register_custom_table' ); // for multisite
>
> function register_custom_table() {
>      global $wpdb;
>      $wpdb->custom_tablename = $wpdb->prefix . 'custom_tablename';
> }
>
>
> regards
> Claudio
> _______________________________________________
> wp-hackers mailing list
> [hidden email]
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>



---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus
_______________________________________________
wp-hackers mailing list
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers