I have this project where I'm converting the old MySQL stuff to PDO. I haven't written a line of PHP in over 7 years, but I'm doing OK on upgrading this project over the last 2 weeks, still rusty on a few things in PHP and MySQL.
My question or problem is that I can't get the parameters :startLetter and :stopLetter to register or prepare in PDO. The :partnerID works fine.
If I hard code it as a test, BETWEEN \'0\' AND \'Z\' it works fine. I did hard code the values in the array below as a test, and I did test the variables as well. I did a search and everyone has pretty much the same thing below.
I'm stumped!
$lc_query =
'SELECT DISTINCT m.image_file, m.manufacturer' .
' FROM product p' .
' INNER JOIN manufacturer m' .
' ON p.mfg_id = m.mfg_id' .
' INNER JOIN products pp' .
' ON p.prod_id = pp.prod_id' .
' WHERE m.manufacturer BETWEEN \'0\' AND \'Z\' ' .
//' WHERE m.manufacturer BETWEEN :startLetter AND :stopLetter ' .
' AND pp.p_id = :partnerID' .
' AND p.avail_type < 3' .
' ORDER BY m.manufacturer';
$lc_params = array(
//':startLetter' => $lc_alphastart . ', PDO::PARAM_STR',
//':stopLetter' => $lc_alphaend . ', PDO::PARAM_STR',
':partnerID' => $go_sitedict['partner_id'] . ', PDO::PARAM_INT'
);
I don't think you can specify the parameter type when using an array to bind during PDOStatement->execute
. All parameters are considered to be string. You would be essentially binding the full string 0, PDO::PARAM_STR
as the start letter and same for stop letter. You can either use PDOStatement->bindParam
to bind individual values and specify the value type or just let PDO treat it as a string when passing an array to ->execute
(which is generally just fine as pretty much every type can implicitly convert to string which means that '0'==0
, even in sql).
So it becomes:
$lc_query =
'SELECT DISTINCT m.image_file, m.manufacturer' .
' FROM product p' .
' INNER JOIN manufacturer m' .
' ON p.mfg_id = m.mfg_id' .
' INNER JOIN products pp' .
' ON p.prod_id = pp.prod_id' .
//' WHERE m.manufacturer BETWEEN \'0\' AND \'Z\' ' .
' WHERE m.manufacturer BETWEEN :startLetter AND :stopLetter ' .
' AND pp.p_id = :partnerID' .
' AND p.avail_type < 3' .
' ORDER BY m.manufacturer';
$lc_params = array(
':startLetter' => $lc_alphastart,
':stopLetter' => $lc_alphaend,
':partnerID' => $go_sitedict['partner_id']
);
//and to execute query...
$stmt=$DB_Connection->prepare($lc_sql);
$stmt->execute($lc_params);