有一个[similar post][1]
,但没有解决方案。
以下代码生成包含占位符名称的 MySQL 查询:
$the_image_itself = "abcde123def.jpg";
$title = "A Book";
$description = "Something to Read";
$the_image_itself = "%".$the_image_itself;
$stmt = $db->prepare("UPDATE nky_posts SET `post_title`=:title, `post_content`=:description WHERE `guid` LIKE :the_image_itself");
$stmt->bindParam(':title', $title);
$stmt->bindParam(':description', $description);
$stmt->bindValue(':the_image_itself', $the_image_itself, PDO::PARAM_STR);
$stmt->execute();
$stmt->debugDumpParams();
echo "<hr/>";
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
$affected_rows = $stmt->rowCount();
结果如下:
start SQL: [105] UPDATE nky_posts SET `post_title`=:title,
`post_content`=:description
WHERE `guid` LIKE :the_image_itself
Params: 3 Key:
Name: [6]
:title paramno=-1
name=[6] ":title"
is_param=1 param_type=2
Key: Name: [12] :description
paramno=-1 name=[12] ":description"
is_param=1 param_type=2
Key: Name: [17] :the_image_itself paramno=-1
name=[17] ":the_image_itself"
is_param=1 param_type=2
这是对象调用:
try{
$db=new PDO('mysql:host=localhost;dbname=viewingr_ssdevwp; charset=utf8',$db_username,$db_password);
}
catch(PDOException $e){
echo 'Error connecting to MySQL!: '.$e->getMessage();
exit();
}
最佳答案
我不知道你从哪里得到的印象 debugDumpParams()
将显示原始 SQL 查询——不会。使用参数化查询时,您在数据库中创建准备好的语句,然后单独发送参数值。它们没有一起发送,这意味着无法打印原始 SQL 查询。
debugDumpParams()
将只显示参数列表、它们的名称、类型等,但不它们的值。但是,您可以做的一件事是检查您的 MySQL 查询日志以查看执行的原始 SQL 查询。
找到日志后,您可以使用以下命令查看最近执行的查询(前提是您具有 SSH 访问权限):
$ sudo tail -f /usr/local/mysql/data/yourQueryLog.log
以上路径只是一个例子。您系统上的实际路径可能不同。
关于php - MySQL 返回 PDO 占位符名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22266155/