function searchPaste($string, $err=false)
{
global $sql;
$buffer = $err?"SELECT * FROM `pastes` WHERE `exposure` = 'public' AND `title` LIKE ? OR `paste` LIKE ? OR `lang` LIKE ?":"SELECT * FROM `pastes` WHERE `exposure` = 'public' AND `title` LIKE ?";
echo $buffer."<br>";
$buffer = $sql->prepare($buffer);
$buffer->execute(array(sprintf("%%%s%%", $string)));
if(!$buffer->rowCount()>0)
return 0;
return $buffer->fetchAll(PDO::FETCH_OBJ);
}
正如您在我的第一个查询中看到的那样,我通过多个案例匹配问题是“?”只处理一次,我不确定如何使用数组来执行所有原因。
任何人都知道我能做什么?
答案 0 :(得分:0)
您需要为查询中的每个占位符提供绑定值/参数。你不得不废除我认为的三元。你可以试试这样的......
$buffer = "SELECT * FROM `pastes` WHERE `exposure` = 'public' AND `title` LIKE ?";
$paramCount = 1;
if (!$err) {
$buffer .= ' OR `paste` LIKE ? OR `lang` LIKE ?';
$paramCount += 2;
}
$stmt = $sql->prepare($buffer);
$stmt->execute(array_fill(0, $paramCount, "%$string%"));
答案 1 :(得分:-1)
function searchPaste($string, $err=false)
{
global $sql;
$sql->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
$query = "SELECT * FROM `pastes` WHERE `exposure` = 'public' AND (`title` LIKE :search";
if ($err) {
$query .= " OR `paste` LIKE :search OR `lang` LIKE :search";
}
$query .= ")";
$stmt = $sql->prepare($query);
$stmt->execute(array("search" => "%$string%")));
return $stmt->fetchAll(PDO::FETCH_OBJ);
}
终于得到了逻辑并修复了查询。