gpt4 book ai didi

php - PDO PHP 在 BetweenPlaceholders 中添加 "AND"

转载 作者:可可西里 更新时间:2023-11-01 07:53:23 28 4
gpt4 key购买 nike

我想在我的 sql 查询的键值对参数之间添加“AND”,但我不知道如何操作。我曾尝试在网上搜索但无法找到解决方案。

       $cdatahome = fetchCategory(array("status"=>"1","home"=>"1")); 

function fetchCategory(array $conditions){
$db = Core::getInstance();

$sql = "SELECT id, title FROM ruj_category WHERE ";

$params = array();
foreach ($conditions as $column => $value) {
if (preg_match('/^[a-z-.]+$/', $column)) {

$sql .= "$column = ?";
$params[] = $value;

}
}
$sql .= " order by title asc";
$res = $db->dbh->prepare($sql);
$res->execute(array_values($params));
$res = $res->fetchAll(PDO::FETCH_ASSOC);

return $res;

最佳答案

$where = array();
foreach ($conditions as $column => $value) {
if (preg_match('/^[a-z-.]+$/', $column)) {
$where[] = "$column = ?";
$params[] = $value;
}
}
$sql .= implode(' AND ', $where);

关于php - PDO PHP 在 BetweenPlaceholders 中添加 "AND",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13866946/

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