Mysql query generating error when using the OR clause

Asked

Viewed 74 times

-1

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

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: He makes a mistake in a ditch 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 the part of the code, line 221 to 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;
}
  • Please show us the table and the error that displays.

  • If this string is formatted with sprintf() he will complain that missing an argument, can place the call of this query?

  • added the call of the consultation tb

  • Query looks good. Try printing your query in php and make sure it’s up to you

  • The error is that only searches the lines with name and ignores the email?

  • @Ricardomoraleida dei 2 edits up with 2 functions, error is in line 237 of code $this->found_data = array_slice($this->example_data, (($current_page - 1) * $per_page), $per_page);

  • no, it gives an error on line 237, php error, does not appear search results

Show 2 more comments

2 answers

1


$form_data is worth null because your call to wpdb->prepare() has two placeholders (%s) and only one value ($like). Then it gives error when forming the string and the query to the bank never comes to be 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,
        ) );
  • Great, thank you!!! Good Tips from WP ein, vlw!

  • hah, neither is. Prepared statements is a more general business of database abstractions. It works more or less the same at all. Long on the road we get to have an eye for things :)

-1

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

in the bank she would be:

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

then Oce should put so, and simplify further by placing single quotes and few double quotes:

"SELECT * FROM '.$table_ucf.' WHERE form LIKE '.$this->form_active.' AND name LIKE '.%s.' OR email LIKE '.%s.'"
  • I forgot to tell you, add relatives too: "SELECT * FROM '.$table_ucf. 'WHERE (form LIKE '.$this->form_active.' AND name LIKE '.%s.') OR email LIKE '.%s.'"

  • didn’t work out... :/

  • Do as @Miasanmia recommended, do an SQL echo for you to see how the string is forming;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.