我试图通过在构建查询之前运行一些逻辑操作来使我的 PDO 查询动态化。
但是,当当前构建查询时,不会返回任何结果,因为我引用的列名用引号括起来(至少我相信这是问题所在),我将如何删除字符串周围的引号?
逻辑运算与查询
// Logical comparison
if($psiA >= $psiB){
$highestPsi = "high_psi";
} else {
$highestPsi = "flow_psi";
}
if($gpmA >= $gpmB){
$highestGpm = "high_gpm";
} else {
$highestGpm = "flow_gpm";
}
var_dump($highestGpm);
var_dump($highestPsi);
// Set up query variables
if( $pVal <= 0 && $gVal <= 0) {
$sql = "SELECT * FROM pumps WHERE pump_type = :pType AND :thePsi >= :pVal AND :theGpm >= :gVal AND pump_category = :cVal";
} else {
$sql = "SELECT * FROM pumps WHERE pump_type = :pType AND :thePsi >= :pVal AND :theGpm >= :gVal AND pump_category = :cVal";
var_dump($sql);
}
// Build and execute query
$stmt = $connection->prepare( $sql );
$stmt->bindParam(':pType', $pType, PDO::PARAM_STR);
$stmt->bindParam(':pVal', $pVal, PDO::PARAM_STR);
$stmt->bindParam(':gVal', $gVal, PDO::PARAM_STR);
$stmt->bindParam(':cVal', $cVal, PDO::PARAM_STR);
$stmt->bindParam(':thePsi', $highestPsi, PDO::PARAM_STR);
$stmt->bindParam(':theGpm', $highestGpm, PDO::PARAM_STR);
$stmt->execute(array(
'pType' => $pType,
'pVal' => $pVal,
'gVal' => $gVal,
'cVal' => $cVal,
'thePsi' => $highestPsi,
'theGpm' => $highestGpm
));
我知道转储正在返回正确的列名,我确定我需要执行某种正则表达式来去除引号以使其正常工作。
任何指针将不胜感激。