有一个[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()
将显示RAW SQL查询的印象 - 它不会。使用参数化查询时,您可以在数据库中创建一个准备好的语句,然后然后单独发送参数值。它们不是一起发送的,这意味着没有办法打印原始SQL查询。
debugDumpParams()
仅显示参数,其名称,类型等的列表,但是不是它们的值。但是,您可以做的一件事是检查您的MySQL查询日志以查看执行的RAW SQL查询。
找到日志后,您可以使用以下命令查看最近执行的查询(前提是您具有SSH访问):
$ sudo tail -f /usr/local/mysql/data/yourQueryLog.log
上面的路径只是一个示例。您的系统上的实际路径可能有所不同。
好吧,"答案"在下面发布,但真正的答案是,我应该停止对这个问题猛击,然后再回到这个问题,这似乎是我要做的最困难的事情之一。在我的痴迷中,我发现了一个神秘的<br/>
,然后在其中一个占位符价值中的一些空间。我最终在变量上进行了substr($var, 0, -6)
,以删除异常,直到注意到我无意中将<br/>
串联到填充变量的线的末端为止。.<br/>
-可能在删除用于测试的输出代码时。
我与Hostmonster Tech支持一起尝试并尝试进入MySQL日志,因为人们说这是唯一的地方可以确定MySQL在使用占位符时的"看到",但他们不会登录。mysql查询,因为文件将在terrabytes中。
凌晨3点或4点,我放弃了。
今天回到了一个新鲜的头上,经过以下步骤确认每个工作:
-
创建一个简单选择不用或占位符的语句:
$sql = "SELECT * FROM nky_posts";
-
使用" ="(不喜欢)添加Where子句,而变量是我知道在DB中的字面意思:
$the_image = "image_url_from_phpMyAdmin"; $sql = "SELECT post_title FROM nky_posts WHERE guid = $the_image";
-
用具有已知价值的单个占位符代替文字变量:
$the_image = "image_url_from_phpMyAdmin"; $stmt->bindParam(':the_image', $the_image, PDO::PARAM_STR); $sql = "SELECT post_title FROM nky_posts WHERE guid = :the_image";
态 用动态变量替换"已知"变量(在这种情况下从XML导致):
basename($oBookNode->getElementsByTagName('actual-link')->item(0)->nodeValue);
(使用basename()函数仅返回WordPress数据库中URL字符串的映像名称)
最终用我的更新语句替换选择语句,添加了两个额外的占位符,以保留要插入的变量。最终代码:
$sql = "UPDATE nky_posts SET post_title=:title, post_content=:description WHERE guid LIKE :the_image"; $stmt = $db->prepare($sql); //foreach loop begins here foreach ($oDOM->getElementsByTagName('item') as $oBookNode) { $the_image = basename($oBookNode->getElementsByTagName('actual-link')->item(0)->nodeValue); $title = $oBookNode->getElementsByTagName('title')->item(0)->nodeValue; $description = $oBookNode->getElementsByTagName('actual-content')->item(0)->nodeValue; //concat % to variable for LIKE clause (probably only needed first one in this case, but...) $the_image = "%" . $the_image . "%"; $stmt->bindParam(':title', $title, PDO::PARAM_STR); $stmt->bindParam(':description', $description, PDO::PARAM_STR); $stmt->bindParam(':the_image_itself', $another_image_itself, PDO::PARAM_STR); $stmt->execute(); //end foreach loop }
感谢您的帮助,大家。
$the_image = "%" . $the_image . "%";
$stmt->bindParam(':the_image', $the_image, PDO::PARAM_STR);
$sql = "SELECT post_title FROM nky_posts WHERE guid LIKE :the_image_itself";
debugDumpParams
功能调用的输出看起来正确。
debugDumpParams
函数不显示绑定参数的值;它仅显示SQL文本以及占位符名称/位置及其各自的数据类型。
我不确定我是否理解您问的问题。
无需在STMT上调用fetchAll
方法,因为它是UPDATE
语句。
请注意,对于rowCount
,MySQL返回语句实际更改的行数,而不是匹配的行数。也就是说,如果已设置的列中的值已经设置为指定值,则MySQL不会"计数"该行被影响。