【问题标题】:PHP / MySQL - prepared statements debugging [duplicate]PHP / MySQL - 准备好的语句调试[重复]
【发布时间】:2015-05-07 07:38:30
【问题描述】:

我有以下代码:

$sql = "SELECT...";
$stmt = $db->prepare($sql);
$stmt->bind_param("<types>", $params...);
$stmt->execute();
$resultSet = $stmt->get_result();

我的问题是我如何准确地看到正在实际执行的查询?

如果我通过 MySQL 客户端直接执行这个查询(没有绑定),效果很好。但是在这段代码中出了点问题,我试图理解是什么。

$stmt->bind_param() 中的类型和参数似乎是正确的,但它返回一个空结果集。

有人吗?

【问题讨论】:

  • 提供您的选择查询
  • 你可以 var_dump 你的声明
  • 同时启用错误,这样您就可以准确地看到问题所在 ini_set('display_errors', 1);
  • var_dump 在哪个对象上?查询是“从向导中选择教育、治疗、调查 WHERE question1 = ? AND question2 = ? AND question3 = ? AND question4 = ? AND question5 = ?”第 1-4 题是整数,第 5 题是字符串
  • 无法检索“最终”查询。你唯一能做的 - 转储查询和绑定参数。

标签: php mysqli prepared-statement


【解决方案1】:

错误处理通常有助于调试代码:

if (false === ($stmt = $db->prepare($sql))) {
    echo 'error preparing statement: ' . $db->error;
} elseif (!$stmt->bind_param("<types>", $params...)) {
    echo 'error binding params: ' . $stmt->error;
} elseif (!$stmt->execute()) {
    echo 'error executing statement: ' . $stmt->error;
}

【讨论】:

  • 查询执行没有问题,但返回一个空结果集。
【解决方案2】:

没有最终查询。服务器接收您的 SQL 语句和您的参数并执行优化。查询本身永远不会与参数一起被字符串化。

【讨论】:

    【解决方案3】:

    你可以试试下面的代码:

    $sql = "SELECT education, treat, investigation FROM wizard WHERE question1 = :question1 AND question2 = :question2 AND question3 = :question3 AND question4 = :question4 AND question5 = :question5";
    $stmt = $db->prepare($sql);
    $stmt->bindParam(':question1', $question1);
    $stmt->bindParam(':question2', $question2);
    $stmt->bindParam(':question3', $question3);
    $stmt->bindParam(':question4', $question4);
    $stmt->bindParam(':question5', $question5);
    $stmt->execute();
    $resultSet = $stmt->get_result();
    

    【讨论】:

      【解决方案4】:

      一个可能的解决方案是实现另一个自定义函数来显示任何错误。请注意,您需要针对您的具体情况进行修改:

      <?php
          function showerror($sql, $types, $params){
              echo "There is an error:\r\n";
              print_r(error_get_last()); //return the error, as well as file and line number
              echo "SQL: ".$sql."\r\nTypes: ".$types;
              var_dump($params);
          }
      
          $sql = "SELECT * FROM users WHERE firstName=? AND lastName=?";
          $types = "sss";
          $params = array("Bob", "Smith");
          if(strlen($types)!=count($params)) showerror($sql, $types, $params); //make sure number of types is equal to the number of params
          if(!$stmt = $db->prepare($sql)) showerror($sql, $types, $params);
          if(!$stmt->bind_param($types, $params...)) showerror($sql, $types, $params);
          if(!$stmt->execute()) showerror($sql, $types, $params);
          $resultSet = $stmt->get_result();
      ?>
      

      【讨论】:

        【解决方案5】:
        <?php
        /* Execute a prepared statement by binding PHP variables */
        $calories = 150;
        $colour = 'red';
        $sth = $dbh->prepare('SELECT name, colour, calories FROM fruit WHERE calories < 
        :calories AND colour = :colour');
        $sth->bindParam(':calories', $calories, PDO::PARAM_INT);
        $sth->bindValue(':colour', $colour, PDO::PARAM_STR, 12);
        $sth->execute();
        
        $sth->debugDumpParams();
        
        ?>
        

        上面的例子会输出:

        SQL:[96] 选择名称、颜色、卡路里 从水果 WHERE 卡路里

        Params:  2
        Key: Name: [9] :calories
        paramno=-1
        name=[9] ":calories"
        is_param=1
        param_type=1
        Key: Name: [7] :colour
        paramno=-1
        name=[7] ":colour"
        is_param=1
        param_type=2
        

        https://www.php.net/manual/en/pdostatement.debugdumpparams.php

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-08-29
          • 2013-07-27
          • 2016-09-10
          • 2011-12-06
          相关资源
          最近更新 更多