搜索查询数组值绑定不起作用

时间:2016-10-26 13:10:15

标签: php arrays search full-text-search

我正在进行搜索查询,我遇到了一点点...所以正如你在下面的代码中看到的那样,我正在为数组添加值以便稍后在脚本中执行它,但它并没有真正起作用...所以当我var_dumped所有这一切时,它会像它应该的那样返回但是:q没有改变为在链接中输入的值。

$query = "SELECT * FROM articles";

$columnsQuery = [];
$values       = [];

if(isset($_GET['q']) && !empty($_GET['q']))
{
$columnsQuery[] = " WHERE MATCH (title) AGAINST (':q' IN NATURAL LANGUAGE MODE)";
$values[":q"]   = $_GET['q'];
}

$fullQuery = $query . implode(" ", $columnsQuery)
   . " ORDER BY id DESC"
   . " LIMIT {$paginator->getLimitSQL()}";

$getArticles = $db->prepare($fullQuery)->execute($values);

4 个答案:

答案 0 :(得分:1)

    $query          = "SELECT * FROM articles";

    $columnsQuery   = [];
    $values         = [];

    if(isset($_GET['q']) && !empty($_GET['q']))
    {
        $columnsQuery[]     = " WHERE MATCH (title) AGAINST (':q' IN NATURAL LANGUAGE MODE)";
        $values["q"]        = $_GET['q'];           // TRY WITHOUT COLON
    }

    $fullQuery = $query . implode(" ", $columnsQuery)
        . " ORDER BY id DESC"
        . " LIMIT {$paginator->getLimitSQL()}";

    $getArticles = $db->prepare($fullQuery)->execute($values);

答案 1 :(得分:1)

您不应在$values["q"] = $_GET['q'];

的地方使用冒号
$query          = "SELECT * FROM articles";

$columnsQuery   = [];
$values         = [];

if(isset($_GET['q']) && !empty($_GET['q']))
{
    $columnsQuery[]     = " WHERE MATCH (title) AGAINST (':q' IN NATURAL LANGUAGE MODE)";
    $values["q"]        = $_GET['q'];           // TRY WITHOUT COLON
}

$fullQuery = $query . implode(" ", $columnsQuery)
    . " ORDER BY id DESC"
    . " LIMIT {$paginator->getLimitSQL()}";

$getArticles = $db->prepare($fullQuery)->execute($values);

答案 2 :(得分:0)

$query = "SELECT * FROM articles";

$values = array();
if(!empty($_GET['q'])) {
  $query .= " WHERE MATCH (title) AGAINST (q IN NATURAL LANGUAGE MODE)";
  $db->bindParam(':q', $_GET['q']);
}

$fullQuery = $query  . " ORDER BY id DESC" . " LIMIT {$paginator->getLimitSQL()}"
$getArticles = $db->prepare($fullQuery)->execute();

答案 3 :(得分:0)

所以过了一会儿我想出来了,你不应该在查询中绑定时使用参数,就像@Poiz指出我不应该在数组中使用冒号

向所有尝试过帮助的人致谢:)