1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[SQL] PHP Show MySQL Search result must contain exact keywords but in any order

Discussão em 'Outras Linguagens' iniciado por Stack, Outubro 13, 2024 às 01:53.

  1. Stack

    Stack Membro Participativo

    I am using this php code for MySQL database query:

    function search_it() {
    if ( isset( $_GET['s'] ) ) {
    global $wpdb;
    $address_table = $wpdb->prefix . 'my_products';
    $search = $_GET['s'];
    $search = trim($search);
    $keywords = explode(" ", $search);
    foreach($keywords as $search) {
    $search = "%{$search}%";
    $where = $wpdb->prepare( 'WHERE name LIKE %s OR price LIKE %d OR id LIKE %d OR market_price LIKE %d OR image_url LIKE %s LIMIT 0,30' , $search, $search, $search, $search, $search );
    }
    $results = $wpdb->get_results( "SELECT * FROM {$address_table} {$where}" );
    return $results;
    }}


    for example my database have these lines

    This is a blue car
    This is a white car
    This is a red car
    This car color is blue
    This is a yellow car
    This is a blue bike
    This car name is bluebird
    Blue car looks awesome



    When i execute the above code, with query blue car then it will show me all results which contain CAR and all results which contain BLUE But i want it show me the only results which contain keyword BLUE and CAR even these keywords are saved in any order in database

    I want it show me only these results, when i search for blue car

    This is a blue car
    This car color is blue
    This car name is bluebird
    Blue car looks awesome


    Above keywords are just for example it would any

    Continue reading...

Compartilhe esta Página