$wdpv returning nothing when using it via AJAX plugin. (2 posts)

  1. CheckMater
    Posted 2 years ago #

    Good day, I have a problem with my plugin because $wdpb returns an error when trying to do any query, my code is as follows:

    function search_engine_callback() {
    	global $wpdb;
    	$keyword = addslashes($_POST['keyword']);
    	//Get all related posts from the WP db...
    	$res = $wpdb->get_results("SELECT id, post_title FROM wp_posts  WHERE post_title = '$keyword'", 'ARRAY_A');

    And it shows me this error:

    WordPress Database Error: [] and the previous query.

    All works fine in the AJAX function but the query doesn't return anything (No matter which query you add) it always return an empty [] as error.

    Any suggestion?

    Thanks in advance!

  2. ianhaycox
    Posted 2 years ago #

    Remove the quotes around ARRAY_A and it should be OK.

    I would suggest you use wpdb->prepare to avoid SQL injection attacks, rather than the extra stripslashes. E.g.

    $sql = "SELECT id, post_title FROM wp_posts WHERE post_title = %s";
    $res = $wpdb->get_results($wpdb->prepare($sql, keyword),ARRAY_A);

Topic Closed

This topic has been closed to new replies.

About this Topic


No tags yet.