【发布时间】:2015-07-13 06:09:24
【问题描述】:
知道为什么返回 false 吗?
用于调用函数的代码:
$product = new Product;
$allProducts = $product->getProducts(12);
我正在调用的函数:
public function getProducts($limit)
{
$values = array($limit);
$statement = $this->conn->prepare('SELECT * FROM sellify_items ORDER BY id DESC LIMIT ?');
$statement->execute($values);
$result = $statement->fetchAll();
if ($result) {
return $result;
}
return false;
}
编辑:更新功能
public function getProducts($limit)
{
$values = array(intval($limit));
$statement = $this->conn->prepare('SELECT * FROM sellify_items ORDER BY id DESC LIMIT ?');
$statement->bindValue(0, $limit, PDO::PARAM_INT);
$statement->execute($values);
$result = $statement->fetchAll();
if ($result) {
return $result;
}
return false;
}
【问题讨论】:
-
上次有,是因为我DSN里的
dbname不好,你检查你的连接了吗? -
连接工作正常。如果我从执行中删除 $values,并替换 ?有一个数字就可以了...
-
@Cherryade 你可能想看看这里:stackoverflow.com/q/2269840/3933332
-
OH WELL PDO 只需用
'包围您的 var,您应该使用$stmt->bindValue(0, $limit, PDO::PARAM_INT);;)
标签: php pdo prepared-statement