我想要转换为使用PDO
代替mysql_
的查询:
$checkLikes = $sdb->query(sprintf("SELECT * FROM `likes`,`users`
WHERE `likes`.`by` = `users`.`idu`
AND `likes`.`by` IN (%s)
ORDER BY `id` DESC
LIMIT %s", $subscriptions, 25));
以上查询使用MySQL来执行。我正在尝试将其转换为PDO:
$checkLikes = $sdb->prepare("SELECT * FROM `likes`,`users`
WHERE `likes`.`by` = `users`.`idu`
AND `likes`.`by` IN :subscriptions
ORDER BY `id` DESC
LIMIT :subscriptions", $subscriptions, 25);
$checkLikes->bindParam(":subscriptions",$subscriptions);
$checkLikes->execute();
上面的代码不起作用,因为我收到了这些错误:
Warning: PDO::prepare() expects at most 2 parameters, 3 given in /home/user/public_html/header.php on line 411
Fatal error: Call to a member function bindParam() on a non-object in /home/user/public_html/header.php on line 412
我做错了什么?我使用bindParam()
将变量绑定到PDO查询。我无法看到错误。
答案 0 :(得分:1)
首先删除要发送到->prepare
语句的第二个和第三个参数,因为只需要查询:
$checkLikes = $sdb->prepare("SELECT * FROM `likes`,`users` WHERE `likes`.`by` = `users`.`idu` AND `likes`.`by` IN (:subscriptions) ORDER BY `id` DESC LIMIT :subscriptions_limit");
然后绑定两个参数并执行查询:
$checkLikes->bindParam(":subscriptions",$subscriptions, PDO::PARAM_STR);
$subscription_limit = 25;
$checkLikes->bindParam(":subscriptions_limit",$subscription_limit, PDO::PARAM_INT);
$checkLikes->execute();
由于您使用的是IN
,因此您还应该确保$subscriptions
是数组值的string
表示,而不是array
中的bindParam()
}