Query MySQL generating error when using the OR clause

-1

I have the query used in WP and it causes an error when I use OR, it follows the usage example:

SELECT * FROM ' . $table_ucf . ' WHERE form LIKE "'. $this->form_active.'" AND name LIKE "%s" OR email LIKE "%s"

If I take the condition after the OR works, example:

SELECT * FROM ' . $table_ucf . ' WHERE form LIKE "'. $this->form_active.'" AND name LIKE "%s"

EDIT:

function getDBData(){
    global $wpdb;
    $table_ucf = $wpdb->prefix . 'ucf';
    //var_dump($this->table_search);
    if ($this->table_search) {
        $like = '%' . $wpdb->esc_like($this->table_search) . '%';
        $form_data = $wpdb->get_results(
            $wpdb->prepare(
                'SELECT * FROM ' . $table_ucf . ' WHERE form LIKE "'. $this->form_active.'" AND name LIKE "%s" OR email LIKE "%s"',
                $like
            )
        , ARRAY_A);
    } else {
        $form_data = $wpdb->get_results(
            $wpdb->prepare(
                'SELECT * FROM ' . $table_ucf . ' ucf WHERE ucf.form = %s',
                $this->form_active,
                '%' . $wpdb->esc_like($this->table_search) . '%'
            )
        , ARRAY_A);
    }
    $this->example_data = $form_data;
}

EDIT: It gives an error in a section below the code;

Warning: array_slice() expects parameter 1 to be array, null given in C:\xampp\htdocs\ekantika\wp-content\plugins\ucf\dx.ucf.table.php on line 237

This is part of code, line 221 through line 244

function prepare_items() {
    $this->process_bulk_action();
    $this->getDBData();

    $columns = $this->get_columns();
    $hidden = array();
    $sortable = $this->get_sortable_columns();
    $this->_column_headers = array($columns, $hidden, $sortable);
    @usort($this->example_data, array(&$this, 'usort_reorder'));

    $per_page = 50;
    $current_page = $this->get_pagenum();
    $total_items = count($this->example_data);


    // only ncessary because we have sample data
    $this->found_data = array_slice($this->example_data, (($current_page - 1) * $per_page), $per_page);
    $this->set_pagination_args(array(
        'total_items' => $total_items, //WE have to calculate the total number of items
        'per_page' => $per_page //WE have to determine how many items to show on a page
    ));

    $this->items = $this->found_data;
}
    
asked by anonymous 25.10.2016 / 13:45

2 answers

1

$form_data is with null value because its call to wpdb->prepare() has two placeholders ( %s ) and only one value ( $like ). Then it gives error in the time to form the string and the query to the bank never gets made.

    $form_data = $wpdb->get_results(
        $wpdb->prepare(
            'SELECT * FROM ' . $table_ucf . ' WHERE form LIKE "%s" AND (name LIKE "%s" OR email LIKE "%s")',
            $this->form_active,
            $like,
            $like,
        ) );
    
25.10.2016 / 18:22
-1

Would not that be the lack of quotation marks? the type of the variables seems to me to be string and need quotes

on the bank it would look like:

SELECT * FROM 'nometabela' WHERE form LIKE 'valorform' AND name LIKE 'valorname' OR email LIKE 'valoremail'

So you should put it like this, and simplify it even more by placing the single quotation mark and a few double quotation marks:

"SELECT * FROM '.$table_ucf.' WHERE form LIKE '.$this->form_active.' AND name LIKE '.%s.' OR email LIKE '.%s.'"
    
25.10.2016 / 14:19