MySQL查询返回PDO没有结果

时间:2017-01-03 20:23:26

标签: php mysql pdo

我有PDO和MySQL的问题。 当我通过phpMyAdmin将该查询发送给MySQL时,它返回490行:

SELECT art.AR_Ref AS AR_Ref, art.AR_Design AS AR_Design, art.AR_PrixVen AS AR_PrixVen, art.AR_CodeBarre AS AR_CodeBarre, af.CT_Num AS CT_Num, af.AF_RefFourniss AS AF_RefFourniss, ct.CT_Intitule AS CT_Intitule, art.CL_No1, art.CL_No2, art.CL_No3, art.CL_No4, MATCH (art.indexation) AGAINST ("+:iso*") AS cpt, art.AR_Design REGEXP "^:iso*" AS commencepar, art.AR_Design REGEXP ".+:iso.*" AS contient
FROM f_article art
    LEFT JOIN f_artfourniss af ON (art.AR_Ref = af.AR_Ref AND af.AF_Principal = 1)
    LEFT JOIN f_comptet ct ON (af.CT_Num = ct.CT_Num AND ct.CT_Type = 1)
WHERE art.AR_Sommeil = '0' AND MATCH (art.indexation) AGAINST (("+:iso*") IN BOOLEAN MODE)
ORDER BY commencepar DESC, contient DESC, cpt DESC, art.AR_Ref

但是使用该代码,根本没有结果:

<?php
$dbCon = new PDO('mysql:host=SERVER_IP; port=3306; dbname=DATABASE',
    'root',
    'PASSWORD',
    array(PDO::ATTR_PERSISTENT => true,
          PDO::ATTR_ORACLE_NULLS => PDO::NULL_EMPTY_STRING,
          PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING));

$sql = 'SELECT art.AR_Ref AS AR_Ref, art.AR_Design AS AR_Design, art.AR_PrixVen AS AR_PrixVen, art.AR_CodeBarre AS AR_CodeBarre, af.CT_Num AS CT_Num, af.AF_RefFourniss AS AF_RefFourniss, ct.CT_Intitule AS CT_Intitule, art.CL_No1, art.CL_No2, art.CL_No3, art.CL_No4, MATCH (art.indexation) AGAINST ("+:saisie1*") AS cpt, art.AR_Design REGEXP "^:saisie2*" AS commencepar, art.AR_Design REGEXP ".+:saisie3.*" AS contient FROM f_article art LEFT JOIN f_artfourniss af ON (art.AR_Ref = af.AR_Ref AND af.AF_Principal = 1) LEFT JOIN f_comptet ct ON (af.CT_Num = ct.CT_Num AND ct.CT_Type = 1) WHERE MATCH (art.indexation) AGAINST (("+:saisie4*") IN BOOLEAN MODE) ORDER BY commencepar DESC, contient DESC, cpt DESC, art.AR_Ref';
$stmt = $dbCon->prepare($sql);
$saisie = 'iso';
$stmt->bindParam(':saisie1', $saisie, PDO::PARAM_STR);
$stmt->bindParam(':saisie2', $saisie, PDO::PARAM_STR);
$stmt->bindParam(':saisie3', $saisie, PDO::PARAM_STR);
$stmt->bindParam(':saisie4', $saisie, PDO::PARAM_STR);
$stmt->execute();
if (($stmt->errorCode() != '00000')){
    var_dump($stmt->errorInfo());
} else {
    while($row = $stmt->fetch(PDO::FETCH_OBJ)) {
        var_dump($row);
    }
    $stmt->closeCursor();
}
$dbCon = null;
?> 

可能是什么问题?

1 个答案:

答案 0 :(得分:0)

感谢Jay Blanchard,

  

这是关于如何在SQL语句中格式化变量。   您应该在bindParam()中为每个变量进行格式化   占位符替换整个变量和任何条件   围绕它。

我找到了解决方案:

<?php
$dbCon = new PDO('mysql:host=SERVER_IP; port=3306; dbname=DATABASE',
    'root',
    'PASSWORD',
    array(PDO::ATTR_PERSISTENT => true,
          PDO::ATTR_ORACLE_NULLS => PDO::NULL_EMPTY_STRING,
          PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING));

$sql = 'SELECT art.AR_Ref AS AR_Ref, '
            . 'art.AR_Design AS AR_Design, '
            . 'art.AR_PrixVen AS AR_PrixVen, '
            . 'art.AR_CodeBarre AS AR_CodeBarre, '
            . 'af.CT_Num AS CT_Num, '
            . 'af.AF_RefFourniss AS AF_RefFourniss, '
            . 'ct.CT_Intitule AS CT_Intitule, '
            . 'art.CL_No1, '
            . 'art.CL_No2, '
            . 'art.CL_No3, '
            . 'art.CL_No4, '
            . 'MATCH (art.indexation) AGAINST (:saisie1) AS cpt, '
            . 'art.AR_Design REGEXP :saisie2 AS commencepar, '
            . 'art.AR_Design REGEXP :saisie3 AS contient '
     . 'FROM f_article art '
            . 'LEFT JOIN f_artfourniss af ON (art.AR_Ref = af.AR_Ref AND af.AF_Principal = 1) '
            . 'LEFT JOIN f_comptet ct ON (af.CT_Num = ct.CT_Num AND ct.CT_Type = 1) '
     . 'WHERE MATCH (art.indexation) AGAINST ((:saisie4) IN BOOLEAN MODE) '
     . 'ORDER BY commencepar DESC, '
            . 'contient DESC, '
            . 'cpt DESC, '
            . 'art.AR_Ref';
$stmt = $dbCon->prepare($sql);
$saisie = 'iso';
$saisie1 = '+' . $saisie . '*';
$saisie2 = '^' . $saisie . '*';
$saisie3 = '.+' . $saisie . '.*';
$saisie4 = '+' . $saisie . '*';
$stmt->bindParam(':saisie1', $saisie1, PDO::PARAM_STR);
$stmt->bindParam(':saisie2', $saisie2, PDO::PARAM_STR);
$stmt->bindParam(':saisie3', $saisie3, PDO::PARAM_STR);
$stmt->bindParam(':saisie4', $saisie4, PDO::PARAM_STR);
$stmt->execute();
if (($stmt->errorCode() != '00000')){
    var_dump($stmt->errorInfo());
} else {
    while($row = $stmt->fetch(PDO::FETCH_OBJ)) {
        var_dump($row);
    }
    $stmt->closeCursor();
}
$dbCon = null;
?>