I have no idea what I am doing wrong here. If i omit thew WHERE clause in this query, the server returns all rows from the database. SO I know that part works. If I use the WHERE CLAUSE i.e.
WHERE name='$name' AND loc='$loc' -
I get results but when I use the bindValue or if I pass the binder in the execute command I get no error and no data
$stmt = "SELECT * FROM table1 LEFT JOIN table2 ON table1.ID=table2.fkID".
" WHERE name = :name AND loc = :loc";
$binder = array(':name' => $name, ':loc' => $loc);
This if the function that takes those parameters
public function fetchData($stmt, $binder = array())
{
$DB = new PDO(........);
$STMT = $DB->prepare($stmt);
foreach ($binder as $key => $value):
$k = (is_numeric($key)) ? $key + 1 : $key;
if (is_int($value)):
$STMT->bindValue($k, (int) $value, PDO::PARAM_INT);
else:
$STMT->bindValue($k, $value, PDO::PARAM_STR);
endif;
endforeach;
try {
$STMT->execute();
return $STMT->fetchAll(PDO::FETCH_ASSOC);
}
catch (PDOException $e) {
die($this->error = $e->getMessage());
}
What am I doing wrong here. Sorry guys i searched all questions similar but I have no answer.
I fixed this by re-writing my code again:
foreach ($binder as $key => $value):
$k = (is_numeric($key)) ? $key + 1 : $key;
$v = $value;
if (is_int($v)):
$STMT->bindValue($k, (int) $v, PDO::PARAM_INT);
else:
$STMT->bindValue($k, $v, PDO::PARAM_STR);
endif;
endforeach;
Note the $v used in the foreach statement instead of the original $value;