Ignoring apostrophes in mysql searches

前端 未结 4 2007
日久生厌
日久生厌 2020-12-21 02:44

I want to take a url that does not have any apostrophes, commas or ampersands in it and match it with a record in a database that may have one of those characters.

F

4条回答
  •  一向
    一向 (楼主)
    2020-12-21 03:36

    It's a little janky, but you could explode the GET and build a WHERE on multiple conditions.

    Something like (untested):

    $name_array = explode("-", $_GET['name']);
    
    $sql_str = "SELECT * FROM the_records WHERE ";
    
    $first_time = true;
    foreach($name_array as $name){
        if ($name != ""){
            if ($first_time){
                $sql_str .= "name LIKE \"%".$name."%\"";
                $first_time = false;
            }
            else {
                $sql_str .= " AND name LIKE \"%".$name."%\"";
            }
        }
    }
    

提交回复
热议问题