Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

So I have this code that queries a custom plugin generated table in the wp database. It works perfectly as is. But I'm trying to adjust it to use $wpdb instead of the $con function I have set up.

Here is the code that works perfectly, but not using $wpdb:

<?php
$q = strtolower($_GET["q"]);
if (!$q) return;
$isArabic = is_arabic($q);

$con=mysql_connect("localhost","admin","*****");
if($con){
    mysql_select_db("arabic_student",$con);
}
else{
    die("Could not connect to database");
}

mysql_query("SET NAMES utf8");
$sql = "SELECT DISTINCT en_word, ar_word FROM wp_enar_words WHERE en_word LIKE '$q%' OR ar_word LIKE '$q%'";
$rsd = mysql_query($sql) or die(mysql_error());

if($isArabic){

    while($rs = mysql_fetch_array($rsd)) {
        $en_word = $rs['en_word'];
        $ar_word = $rs['ar_word'];

        echo "$ar_word\n";
}
}else{
    while($rs = mysql_fetch_array($rsd)) {
        $en_word = $rs['en_word'];
        $ar_word = $rs['ar_word'];

        echo "$en_word\n";

}
}
?>

Here is my noob attempt to use $wpdb:

global $wpdb;
$sql = $wpdb->get_results("SELECT en_word, ar_word FROM wp_enar_words WHERE en_word LIKE   '$q%' OR ar_word LIKE '$q%'");
$rsd = $wpdb->query('$sql');

But it gets a Fatal error: Call to a member function get_results() on a non-object in.... So maybe missing something obvious but hoping I could get some guidance on how to port my existing code to using $wpdb.

Thanks in advance!


UPDATE: so I'm starting to think that the problem is that this code is being called with a jquery plugin. I'm using jquery autocomplete and calling the above php code to populate the form autocomplete:

$("#search_words").autocomplete("<?php bloginfo('template_directory'); ?>/search_ac.php", {});

Could this be the problem? I'm confused because my original code that directly calls the database works perfectly, but once I use $wpdb, it no longer functions

share|improve this question
    
Are you using this inside a plugin or theme? If neither, then that'll be why you get that error. If inside a plugin/theme, is this code going inside a function attached to a particular hook? –  t31os Feb 7 '11 at 13:20
    
the code is in my theme folder, but what I'm starting to think is the problem is that it's being called via javascript. I'm using the autocomplete jquery plugin and it references the file with all the above code $("#search_words").autocomplete("<?php bloginfo('template_directory'); ?>/search_ac.php", {}); –  Justin Feb 7 '11 at 16:54
    
My current guess would be - you are not loading WP engine correctly and $wpdb variable is not instantiated with object of wpdb class. –  Rarst Feb 7 '11 at 16:59
    
I use $wpdb in a wp plugin on my site and it is working there... just here in my theme file is where the problem is. But if I was loading the WP engine incorrectly, how would I fix that? –  Justin Feb 7 '11 at 17:09
    
Loading PHP file directly with your JS request is not the same as loading that file during generic WP request. See answer by Denis and AJAX in Plugins in Codex. –  Rarst Feb 7 '11 at 19:52

2 Answers 2

up vote 3 down vote accepted

Your error message means that $wpdb in your code is currently pointing to a null pointer. Either WP is not loaded, or you're missing a simple global $wpdb; statement, or both.

If you need to load WP, include wp-load.php.

Or better, in your JS, use ajaxurl:

url = ajaxurl + '?action=youraction'

alongside (assuming the above code):

add_action('wp_ajax_youraction', 'yourcode');

there's an unauthenticated version of the same (see wp-admin/admin-ajax.php)

share|improve this answer
    
Thanks Denis and Rarst for that clarification. I haven't tested to see if this works, and I may have to revisit this toward the end of my project, since I have it working by directly connecting to the db. Using $wpdb would just be a bonus best practice. But thank you, now I know where the problem lies when I revisit this. –  Justin Feb 8 '11 at 7:26

Try this:

$sql = $wpdb->prepare( "SELECT en_word, ar_word FROM {$wpdb->prefix}enar_words WHERE en_word LIKE '%1\$s%%' OR ar_word LIKE '%1\$s%%'", $q );
$results = $wpdb->get_results( $sql );
share|improve this answer
    
Thanks for your help... but I still get Fatal error: Call to a member function prepare() on a non-object in.... –  Justin Feb 7 '11 at 16:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.