Combine PHP prepared statments with LIKE

后端 未结 6 1784
南方客
南方客 2020-12-02 00:12

Anyone know how to combine PHP prepared statements with LIKE? i.e.

\"SELECT * FROM table WHERE name LIKE %?%\";

6条回答
  •  一向
    一向 (楼主)
    2020-12-02 00:48

    The % signs need to go in the variable that you assign to the parameter, instead of in the query.

    I don't know if you're using mysqli or PDO, but with PDO it would be something like:

    $st = $db->prepare("SELECT * FROM table WHERE name LIKE ?");
    $st->execute(array('%'.$test_string.'%'));
    

    EDIT :: For mysqli user the following.

    $test_string = '%' . $test_string . '%';
    $st->bind_param('s', $test_string);
    $st->execute();
    

提交回复
热议问题