• I am building a custom plugin. It has it’s own DB table (columns: id,postid,unique,url).

    I built a function which contains this:

    global $wpdb;
    		$table = $wpdb->prefix."mypluginstable";
    
    		$unique = $_GET['uniqueid'];
    		$query = $wpdb->prepare("SELECT * FROM ".$table." WHERE unique=".$unique."");
    		$query2 = $wpdb->get_results($query);
    		print_r($query2);

    I’ve added the function’s to INIT like:
    add_action('init', 'my_cool_function_name');

    However I get this error displayed instead of the print_r:

    WordPress database error: [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘unique=680605’ at line 1]
    SELECT * FROM wp_mypluginstable WHERE unique=680605

    I’ve tried using single quotes (‘) around the word unique (in unique=…) and also the $unique variable in the Mysql command.

Viewing 1 replies (of 1 total)
  • Thread Starter Dzhuneyt

    (@mrgee)

    This has been fixed by adding backticks (not single quotes) around the static variables in the Mysql query. In the above example you would need to surround the table name, and column name, but not the $unique variable. It is to be surrounded with single quotes and not backticks. I can’t post an example here, because the WordPress forums process content inside backticks as code quotes. This character is usually located to the left of the key for number one on your keyboard.

Viewing 1 replies (of 1 total)

The topic ‘Problem with $wpdb->get_results’ is closed to new replies.