gpt4 book ai didi

php - 带有 LIKE '% ? %' PHP 的 SQL 参数化查询

转载 作者:行者123 更新时间:2023-11-29 04:00:23 25 4
gpt4 key购买 nike

我在 php 中有一个搜索功能,并使用参数化查询创建它以确保其安全。

$words = $_POST['words']//words is the form that has the words submitted by the user 
$array = explode(',', $words);
$con = mysqli_connect("localhost","user","pass","database");

$stmt = $con->prepare(" SELECT column_name FROM table WHERE column_name LIKE ?")
foreach($array as $key) { //searches each word and displays results
$stmt->bind_param('s', $key)
$stmt->execute();
$result = $stmt->get-result();

while($row = $result->fetch_assoc(){
echo $row["column_name"]
}
}

但是我希望$stmt声明是

  $stmt = $con->prepare(" SELECT column_name FROM table WHERE column_name LIKE '%?%' ")

否则人们必须输入 column_name 的整个值才能找到它。

最佳答案

您可以使用 CONCAT() ,像这样:

LIKE CONCAT ('%', ?, '%')

关于php - 带有 LIKE '% ? %' PHP 的 SQL 参数化查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29482147/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com