PHP PDO 查询不带参数但不带参数
PHP PDO query works without parameters but not with
我有一个非常简单的查询,在我不使用参数时也能正常工作。有了参数,就returns没什么了。其他人在这里发布了同样的问题:
Query with input parameters doesn't work whereas direct query works
然而没有人回答。下面是我的代码。
require_once('database.class.php');
class Plan extends Database {
public function getBenefitAmounts($plan_id, $group_id, $level) {
$sql = 'SELECT DISTINCT benefit FROM rates WHERE plan_id = :plan AND group_id IS NULL AND `level` = :lvl';
$params = array(':plan'=>896, ':lvl'=>1);
$this->sqlQuery($sql, $params);
// $sql = 'SELECT DISTINCT benefit FROM rates WHERE plan_id = 896 AND group_id IS NULL AND `level` = 1';
// $this->sqlQuery($sql);
$results = $this->sth->fetchAll(PDO::FETCH_COLUMN);
$options = '';
foreach ($results as $value) {
$options .= '<option value="' . $value . '">$' . $value . '</option>';
}
return $options;
}
}
数据库中class:
public function sqlQuery($sql, $values_to_bind=null) {
$this->sth = $this->pdo->prepare($sql);
if (isset($values_to_bind)) {
foreach ($values_to_bind as $param => $value) {
$this->sth->bindValue($param, $value);
}
}
$success = $this->sth->execute();
if (!$success) {
$arr = $this->$sth->errorInfo();
print_r($arr);
}
}
从第一个代码片段中注释掉的代码工作得很好,但是有参数,它returns什么都没有。 getBenefitAmounts
函数是从另一个 PHP 文件调用的,该文件是使用 JQuery get.
调用的
为什么不使用 bindParam 绑定参数?
$plan = 896;
$lvl = 1;
$sth = $dbh->prepare("SELECT DISTINCT benefit FROM rates WHERE plan_id = :plan AND group_id IS NULL AND `level` = :lvl");
$sth->bindParam(":plan", $plan);
$sth->bindParam(":lvl", $lvl);
$sth->execute();
$r = $sth->fetchAll();
您是否尝试添加 bindValue() 可选的第三个参数。它可以是 PDO::PARAM_INT、PDO::PARAM_STR 等。尝试调试看看是否有帮助。
不知道你为什么这么喜欢try...catch
如果你用的是无感码。因为这个技术: } catch (PDOException $e) { throw new PDOException($e);}
和 } catch (PDOException $e) {;}
的意思一样,就像你要求 php 如果抓到什么也不做。如果你什么都不做,你为什么要求抓住以防万一?
现在我猜测如何修复您的代码:
public function sqlQuery($sql, $values_to_bind=null) {
$this->sth = $this->pdo->prepare($sql);
if (isset($values_to_bind)) {
foreach ($values_to_bind as $param => $value) {
$this->sth->bindValue($param, $value);
}
}
$success = $this->sth->execute();
if (!$success) {
$arr = $this->$sth->errorInfo();
print_r($arr);
}
}
顺便说一句,您使用 $this->sth = $this->pdo->prepare($sql);
的方式意味着您的 sqlQuery
函数是某些 class 的方法,您没有向我们展示。你的第一段代码在 class 之外的某个地方?如果您 post 完整版本的代码,而不仅仅是您认为涉及的行,那会更好。
在这里你可以切换到常规方式:
//$results = $this->sth->fetchAll(PDO::FETCH_COLUMN); //you don't need it
$options = '';
while ($row = $this->sth->fetch(PDO::FETCH_ASSOC)) {
$options .= '<option value="' . $row['benefit'] . '">$' . $row['benefit'] . '</option>';
}
我有一个非常简单的查询,在我不使用参数时也能正常工作。有了参数,就returns没什么了。其他人在这里发布了同样的问题: Query with input parameters doesn't work whereas direct query works
然而没有人回答。下面是我的代码。
require_once('database.class.php');
class Plan extends Database {
public function getBenefitAmounts($plan_id, $group_id, $level) {
$sql = 'SELECT DISTINCT benefit FROM rates WHERE plan_id = :plan AND group_id IS NULL AND `level` = :lvl';
$params = array(':plan'=>896, ':lvl'=>1);
$this->sqlQuery($sql, $params);
// $sql = 'SELECT DISTINCT benefit FROM rates WHERE plan_id = 896 AND group_id IS NULL AND `level` = 1';
// $this->sqlQuery($sql);
$results = $this->sth->fetchAll(PDO::FETCH_COLUMN);
$options = '';
foreach ($results as $value) {
$options .= '<option value="' . $value . '">$' . $value . '</option>';
}
return $options;
}
}
数据库中class:
public function sqlQuery($sql, $values_to_bind=null) {
$this->sth = $this->pdo->prepare($sql);
if (isset($values_to_bind)) {
foreach ($values_to_bind as $param => $value) {
$this->sth->bindValue($param, $value);
}
}
$success = $this->sth->execute();
if (!$success) {
$arr = $this->$sth->errorInfo();
print_r($arr);
}
}
从第一个代码片段中注释掉的代码工作得很好,但是有参数,它returns什么都没有。 getBenefitAmounts
函数是从另一个 PHP 文件调用的,该文件是使用 JQuery get.
为什么不使用 bindParam 绑定参数?
$plan = 896;
$lvl = 1;
$sth = $dbh->prepare("SELECT DISTINCT benefit FROM rates WHERE plan_id = :plan AND group_id IS NULL AND `level` = :lvl");
$sth->bindParam(":plan", $plan);
$sth->bindParam(":lvl", $lvl);
$sth->execute();
$r = $sth->fetchAll();
您是否尝试添加 bindValue() 可选的第三个参数。它可以是 PDO::PARAM_INT、PDO::PARAM_STR 等。尝试调试看看是否有帮助。
不知道你为什么这么喜欢try...catch
如果你用的是无感码。因为这个技术: } catch (PDOException $e) { throw new PDOException($e);}
和 } catch (PDOException $e) {;}
的意思一样,就像你要求 php 如果抓到什么也不做。如果你什么都不做,你为什么要求抓住以防万一?
现在我猜测如何修复您的代码:
public function sqlQuery($sql, $values_to_bind=null) {
$this->sth = $this->pdo->prepare($sql);
if (isset($values_to_bind)) {
foreach ($values_to_bind as $param => $value) {
$this->sth->bindValue($param, $value);
}
}
$success = $this->sth->execute();
if (!$success) {
$arr = $this->$sth->errorInfo();
print_r($arr);
}
}
顺便说一句,您使用 $this->sth = $this->pdo->prepare($sql);
的方式意味着您的 sqlQuery
函数是某些 class 的方法,您没有向我们展示。你的第一段代码在 class 之外的某个地方?如果您 post 完整版本的代码,而不仅仅是您认为涉及的行,那会更好。
在这里你可以切换到常规方式:
//$results = $this->sth->fetchAll(PDO::FETCH_COLUMN); //you don't need it
$options = '';
while ($row = $this->sth->fetch(PDO::FETCH_ASSOC)) {
$options .= '<option value="' . $row['benefit'] . '">$' . $row['benefit'] . '</option>';
}