1

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...

4
  • 1
    You have to use prepare() and execute() instead of query(). Commented Feb 9, 2017 at 10:20
  • You put the values in $array_sql, but then you don't do anything with it! Shouldn't you prepare the query! Commented Feb 9, 2017 at 10:21
  • It is only when i wrote and read again this post that I saw this stupid mistake... Indeed, I have several conditions and I simplified it to post on the forum, then I could easily see. Sorry for wasted time... Commented Feb 9, 2017 at 10:24
  • You should always check the results of an operation that can fail before assuming they've returned what they should when they succeed. PDO query returns boolean false if it couldn't execute the query (unless you've configured PDO to throw exceptions on failure). You need to check for that. As for why it failed it's probably due to a syntax error in your generated SQL string Commented Feb 9, 2017 at 10:30

1 Answer 1

1

You did not assign the value

 $req= $bdd->prepare($sql);
    $req->execute(array(':numeroCommande'=> $$_POST['numeroCommande'],
                               ':numeroOT'=>$$_POST['numeroOT']));
        $data =  $req->fetch();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.