-1
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);
}

正如您在我的第一个查询中看到的,我通过多个案例匹配问题是“?” 只处理一次,我不确定如何使用数组为所有原因执行此操作。

有谁知道我能做什么?

4

2 回答 2

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%"));
于 2013-11-12T23:01:50.177 回答
-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);
}

终于得到了逻辑并修复了查询。

于 2013-11-12T23:05:42.980 回答