变量SQL查询=>致命错误,获取布尔值

时间:2017-02-09 10:14:32

标签: php sql

这是我的代码(不要害怕为$ _POST元素赋值,它只适用于测试):

$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();

我得到:"Fatal error: Call to a member function fetch() on boolean"

我试图显示我的数组和变量$sql,以检查是否没有异常。所以我将$data->$req->fetch()替换为:

print_r($array_sql); ?> <br /> <?php
echo ($sql);

以下是我在浏览器中获得的内容:

Array ( [numeroCommande] => 531351351 [numeroOT] => JC05 )

SELECT * FROM voyages WHERE 1 AND numeroCommande = :numeroCommande AND numeroOT = :numeroOT

我无法看到问题出在哪里......

1 个答案:

答案 0 :(得分:1)

您没有指定值

 $req= $bdd->prepare($sql);
    $req->execute(array(':numeroCommande'=> $$_POST['numeroCommande'],
                               ':numeroOT'=>$$_POST['numeroOT']));
        $data =  $req->fetch();