使用bindValue的PHP:PDO(mysql)返回错误的结果
到目前为止,我得到的代码没有返回我所需要的。我看不出这里出了什么问题。 通过对代码进行“糟糕”的调整,它可以提供正确的输出,但我认为最好是正确地进行调整。 为什么它不起作用 输出错误:数组([L]=>L) 右输出:数组([L]=>9) 此代码给出了错误的输出:使用bindValue的PHP:PDO(mysql)返回错误的结果,php,pdo,Php,Pdo,到目前为止,我得到的代码没有返回我所需要的。我看不出这里出了什么问题。 通过对代码进行“糟糕”的调整,它可以提供正确的输出,但我认为最好是正确地进行调整。 为什么它不起作用 输出错误:数组([L]=>L) 右输出:数组([L]=>9) 此代码给出了错误的输出: public function getStockByID_SIZE($size, $stockId){ try { $this->_dbh->setAttribute( PDO::ATTR_ERRMODE,
public function getStockByID_SIZE($size, $stockId){
try {
$this->_dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$sth=$this->_dbh->prepare("SELECT :size from stock WHERE id_product = :stockId");
$sth->bindValue(':size', $size, PDO::PARAM_STR);
$sth->bindValue(":stockId", $stockId);
$sth->execute();
$result = $sth->fetch(PDO::FETCH_ASSOC);
return $result;
} catch (PDOException $e) {
return "Error";
}
}
相同的代码,但带有(错误)调整,返回正确的代码:
$sth=$this->_dbh->prepare("SELECT $size from stock WHERE id_product = :stockId");
//与之相比:
$sth=$this->_dbh->prepare("SELECT :size from stock WHERE id_product = :stockId");
$sth->bindValue(':size', $size); //use of PDO::PARAM_STR doenst matter for outcome
$sth=$this->\u dbh->prepare(“从库存中选择L,其中id\u product=:stockId”)代码>是正确的方法
您的方法就是从库存中选择'L',其中id\u product=:stockId
,这将为您提供结果'L'
您可以在验证$size
后执行以下操作
$sth=$this->\u dbh->prepare(“从库存中选择$size,其中id\u product=:stockId”)代码>但我会改变,我可以使用$size,但可以吗?。我来调整温度op@Rob$size是validate是什么意思?$size在进入类函数之前是validate。它是否也应该存在于类函数本身中?@Rob然后您可以在sql中使用它。