Here is my code (don't be scared about giving a value to $_POST elements, it's only for tests) :
$array_sql = array();
$sql = "SELECT * FROM voyages WHERE 1";
$_POST['numeroCommande'] = 531351351;
if (!empty($_POST['numeroCommande']))
{
$sql .= " AND numeroCommande = :numeroCommande";
$array_sql['numeroCommande'] = $_POST['numeroCommande'];
}
$_POST['numeroOT'] = 'JC05';
if (!empty($_POST['numeroOT']))
{
$sql .= " AND numeroOT = :numeroOT";
$array_sql['numeroOT'] = $_POST['numeroOT'];
}
$req = $bdd->query($sql . ' ORDER BY dateMinChargement DESC');
$data = $req->fetch();
I get : "Fatal error: Call to a member function fetch() on boolean"
I tried to display my array and the variable $sql in order to check if there was no anomaly. So I replaced $data->$req->fetch() by :
print_r($array_sql); ?> <br /> <?php
echo ($sql);
Here is what I get in the browser :
Array ( [numeroCommande] => 531351351 [numeroOT] => JC05 )
SELECT * FROM voyages WHERE 1 AND numeroCommande = :numeroCommande AND numeroOT = :numeroOT
I can't see where is the problem...
$array_sql, but then you don't do anything with it! Shouldn't you prepare the query!