Cha*_*rch 28
%符号需要包含您分配给参数的变量,而不是查询中的变量.
我不知道你是使用mysqli还是PDO,但是使用PDO它会是这样的:
$st = $db->prepare("SELECT * FROM table WHERE name LIKE ?");
$st->execute(array('%'.$test_string.'%'));
Run Code Online (Sandbox Code Playgroud)
编辑::对于mysqli用户以下.
$test_string = '%' . $test_string . '%';
$st->bind_param('s', $test_string);
$st->execute();
Run Code Online (Sandbox Code Playgroud)
您可以使用相应sql数据库的串联运算符:
# oracle
SELECT * FROM table WHERE name LIKE '%' || :param || '%'
# mysql
SELECT * from table WHERE name LIKE CONCAT('%', :param, '%')
Run Code Online (Sandbox Code Playgroud)
我不熟悉其他数据库,但他们可能有一个等效的函数/运算符.