diff --git a/library/think/db/Driver.php b/library/think/db/Driver.php index 5b56b6fe..4c70d834 100644 --- a/library/think/db/Driver.php +++ b/library/think/db/Driver.php @@ -167,11 +167,9 @@ abstract class Driver return false; } - $this->queryStr = $sql; + // 根据参数绑定组装最终的SQL语句 + $this->queryStr = $this->getBindSql($sql, $bind); - if ($bind) { - $this->queryStr .= '[ ' . print_r($bind, true) . ' ]'; - } if ($fetch) { return $this->queryStr; } @@ -213,11 +211,9 @@ abstract class Driver return false; } - $this->queryStr = $sql; + // 根据参数绑定组装最终的SQL语句 + $this->queryStr = $this->getBindSql($sql, $bind); - if ($bind) { - $this->queryStr .= '[ ' . print_r($bind, true) . ' ]'; - } if ($fetch) { return $this->queryStr; } @@ -249,6 +245,27 @@ abstract class Driver } } + /** + * 组装最终的SQL语句 便于调试 + * @access public + * @param string $sql 带参数绑定的sql语句 + * @param array $bind 参数绑定列表 + * @return string + */ + protected function getBindSql($sql, array $bind = []) + { + if ($bind) { + foreach ($bind as $key => $val) { + $val = is_array($val) ? $val[0] : $val; + // 判断占位符 + $sql = is_numeric($key) ? + substr_replace($sql, $val, strpos($sql, '?'), 1) : + str_replace(':' . $key, $val, $sql); + } + } + return $sql; + } + /** * 参数绑定 * 支持 ['name'=>'value','id'=>123] 对应命名占位符