I have read many many posts on SO and several accepted answers that don't work for me. I have been looking at the code for so long I can't see straight anymore (over several weeks).
I need to query a MySQL database table based on 2 columns. The second column can contain a number or be NULL. The most accepted answer I found was this: bindValue(':terri', null, PDO::PARAM_INT);, but it doesn't work for me and for the life of me I can't see why.
Query:
$sql = "SELECT s.`id`, s.`php_tz`, t.`offset`, t.`description`
FROM `{$pdo->db_name}`.`timezones_std` AS s
INNER JOIN `{$pdo->db_name}`.`timezones` AS t ON s.`std_tz_id` = t.`id`
WHERE s.`country_id` = :ctry AND s.`territory_id` = :terri
ORDER BY t.`offset` ASC, t.`description` ASC, SUBSTRING_INDEX(s.`php_tz`, '/', -1) ASC;
";
Bind:
$pdo->bind(":terri", !empty($_POST["terri"]) ? $_POST["terri"] : null, PDO::PARAM_INT);
Bind Function:
public function bind($param, $value = null, $type = null)
{
if(is_array($param) && is_null($value) && is_null($type))
{
foreach($param as $key => $value)
{
$this->bind($key, $value);
} // Closing brace for foreach($param as $key => $value)
} // Closing brace for if(is_array($param) && is_null($value) && is_null($type))
else
{
if(is_null($type))
{
switch(true)
{
case is_int($value):
$type = PDO::PARAM_INT;
break;
case is_bool($value):
$type = PDO::PARAM_BOOL;
break;
case is_null($value):
$type = PDO::PARAM_NULL;
break;
default:
$type = PDO::PARAM_STR;
break;
}
}
$this->stmt->bindValue($param, $value, $type);
} // Closing brace for if(is_array($param) && is_null($value) && is_null($type)) else
}
I know my bind function contains a is_null case but that should not be in play because I pass the 3rd parameter. As posted the code runs with no errors but does not return the correct records.
Can anyone see what I can't.
Thanks for any and all help,
Charles