PDO 准备和执行查询 returns 总是出错

PDO prepare and execution of query returns always error

我有一个功能,

对我来说 return 总是一个错误,即使在我通过查询时在 phpmyadmin 中有一个结果。

我想我的查询有问题,但我不知道是什么。我曾经做我的代码做 mysql_real_escape_string,然后我转向 PDO,他们告诉我我应该为转义 GET 变量做准备,所以我试着去做。

下面是我的查询

public static function getDetailService($param) {
    global $bdd;
    $detail = $bdd->prepare('SELECT
            spb_services.spb_services__name,
            spb_services.spb_services__description,
            spb_services.spb_services__banner,
            spb_services.spb_services__tabs,
            spb_services.spb_services__category
        FROM spb_services
        WHERE spb_services.spb_services__name LIKE :service');

    $detail->bindValue(':service', $_GET[$param], PDO::PARAM_STR);
    $resultat = $detail->fetchAll(PDO::FETCH_ASSOC);

    //var_dump($_GET[$param]);

    $detail->debugDumpParams();
    $lignes = $detail->fetchColumn();

    //var_dump($lignes);
    $detail = $detail->fetchAll(PDO::FETCH_ASSOC);
    $retour = ($lignes > 0) ? array('status' => 'ok') : array('status' => 'error');
    var_dump($retour);
}

当我调用函数时:$service = nosServices::getDetailService('service');

var_dump($_GET[$param])return 的 Var 转储对我来说是什么(来自 url)

然后我做了$detail->debugDumpParams();

我在我的本地主机 phpmyadmin 中通过了查询,它 return 对我来说是预期的,但在使用 PDO 时不是。

我想有一点不对,但我不知道是什么。

这个return没有错误,但是总是错误,好像没有num_rows_result

总结一下问题,GEt return 是预期的,但是当我们去查询时,它 return 没有结果(除了在我的 phpmyadmin 中复制并粘贴查询)

如有任何帮助,我们将不胜感激

编辑:按照其他用户的预期进行修改

需要更正参数绑定。

您在为参数提供值时缺少 :

更正后的代码:

...
WHERE spb_services.spb_services__name LIKE  :service');
        $detail->bindValue(':service', $_GET[$param], PDO::PARAM_STR);
...

唯一的问题在你的代码中忘记在获取数据之前执行查询它会是

 $detail->bindValue(':service', $_GET[$param], PDO::PARAM_STR);
 $detail->execute();// execute it first
 $resultat = $detail->fetchAll(PDO::FETCH_ASSOC);
 print_r($resultat);

您的代码中发生了一些奇怪的事情。

我评论了我认为事情需要改变的地方

public static function getDetailService($param) {
    global $bdd;    // bad practice, see later suggestion
    $detail = $bdd->prepare('SELECT
                     spb_services.spb_services__name,
                     spb_services.spb_services__description,
                     spb_services.spb_services__banner,
                     spb_services.spb_services__tabs,
                     spb_services.spb_services__category
                FROM spb_services
                WHERE spb_services.spb_services__name LIKE :service');

    // $GET? I assume you want to use the `$param you pass as a param to this function
    //$detail->bindValue(':service', $_GET[$param], PDO::PARAM_STR);

    // a LIKE normally requires a string like '%something%'
    // or 'something%'
    // DO we assume you passed $param with the wildcards already applied?
    $detail->bindValue(':service', $param, PDO::PARAM_STR);

    // now the prepared query must be executed
    $detail->execute();

    // fetchAll returns ALL the result set into an array
    $resultat = $detail->fetchAll(PDO::FETCH_ASSOC);

    // as you are using a LIKE we have to assume there will be more 
    // than one row returned. 

    // fetchColumn makes no sense here
    //$lignes = $detail->fetchColumn();

    // You already did a fetchAll so this makes no sence
    //$detail = $detail->fetchAll(PDO::FETCH_ASSOC);

    // as all you appear to be doing is testing if one or more rows are returned
    // then all you need to do is coumt the occurances in the $resultat array

    $retour = (count($resultat) > 0) ? array('status' => 'ok') : array('status' => 'error');

    // Now you need to return something
    return $retour;

}

It is also bad practice to use a global in a class method as it breaks the encapsulation, it is better practice to pass something like that as a parameter.

EG 这个

public static function getDetailService($param) {
    global $bdd;

变成这样

public static function getDetailService($bdd, $param) {
    // global $bdd;   <-- no longer needed

或者如果整个 class 都需要它,那么将其设为 class 属性!

If I am right and all you want to know from this method is if something exists, a SELECT COUNT(id) as cnt would be a more efficient way of doing that, but lets leave that for another day as it would also chnage how you get at the result and write the rest of this code

Final Note: You are doing no error checking after any of the PDO statement taht could possibly go wrong. Almost all of these PDO statements return a status that if false you should display yourself a PDO generated error message See ErrorInfo

您忘记执行 $detail->execute();处理 SQL 请求

$detail->bindValue(':service', $_GET[$param], PDO::PARAM_STR);
if( ! $detail->execute()) {
   die('Invalid Mysql Query!');
}
$resultat = $detail->fetchAll(PDO::FETCH_ASSOC);

希望对您有所帮助:)