一步一步MySql查询到PDO

时间:2015-04-07 18:20:48

标签: php pdo

我正在试图弄清楚如何在这样的PDO中构建查询

//...
$sql = array();
$sql[] = "SELECT * FROM `posts` WHERE `completed` = '1'";
if($this->is($_GET, 'category')) {
    $sql['category'] = "AND `category` = '".$_GET['category']."'";
}
if($this->is($_GET, 'tags')) {
    $sql['tags'] = "AND `tags` LIKE '%".$_GET['tags']."%'";
}
$sql[] = "ORDER BY `id` DESC LIMIT ".$offset.", ".$rows_per_page;
$query = $this->query(implode(" ", $sql));
//...

我尝试了类似的东西..

$sql = array();
$sql[] = "SELECT * FROM `posts` WHERE `completed` = :completed";
if($this->is($_GET, 'category')) {
    $sql['category'] = "AND `category` = :category";
}
$sql[] = "LIMIT 0, 5";

$this->db->query(implode(" ", $sql));
$this->db->bind(array(
    ':completed' => 1,
    ':category' => $this->is($_GET, 'category')
));
$fetch = $this->db->fetchAll();
print_r($fetch);

但是有一个错误,表示我无法绑定不存在的变量“SQLSTATE [HY093]:参数号无效:绑定变量数与令牌数不匹配” ......通过一些研究,我发现在查询之前我无法绑定

..所以..你知道我该怎么办?

0 个答案:

没有答案