I am trying to make my PDO query dynamic by running some logical operations before building my query.
However when the query is currently built no results are returned because the column name I am referencing in surrounded in quotations ( at least I believe this is the problem ), how would I go about removing quotation marks surrounding my string?
Logical Operation & Query
// 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
));
I know that the dump is returning the correct column names, I am certain that I need to perform some sort of regex to strip the quotes for this to work.
Any pointers would be most appreciated.