gpt4 book ai didi

php - PHP + MySQL + PDO 中的单引号转义

转载 作者:行者123 更新时间:2023-11-30 22:49:12 25 4
gpt4 key购买 nike

我需要有关转义单引号的帮助。我已经检查了很多 Material ,但我还不够精明,无法使用它。我的 PHP 代码如下。

<?php
$db = new PDO("mysql:host=localhost;dbname=tool", 'root', 'password');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, 1);
$rne = file_get_contents('/path/export.txt');

$sql = "
LOAD DATA INFILE '/path/UCBTexport.txt'INTO TABLE tool_tbl FIELDS TERMINATED BY ',' ( Bug_ID, Date_Tool_Ran, Headline, Submitted_By, Create_Modify_Date, Severity, Status );
UPDATE `tool_tbl` SET `Release_Notes`= '$rne' WHERE id = LAST_INSERT_ID()
";

$stmt = $db->prepare($sql);
$stmt->execute();
$i = 0;

do {
$i++;
}
while ($stmt->nextRowset())
;

$error = $stmt->errorInfo();
if ($error[0] != "00000")
{
echo "Query $i failed: " . $error[2];
}
else
{
echo "inserted Successfully";
}
die();

我的两个查询都有偶尔包含所有类型特殊字符的文本。如何在这段代码中使用 mysql_real_escape_string 来转义特殊字符?

最佳答案

试试这个,它可能对你有帮助

<?php
$db = new PDO("mysql:host=localhost;dbname=tool", 'root', 'password');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, 1);
$rne = file_get_contents('/path/export.txt');

$sql = <<<SQL
LOAD DATA INFILE '/path/UCBTexport.txt' INTO TABLE tool_tbl FIELDS TERMINATED BY ',' ( Bug_ID, Date_Tool_Ran, Headline, Submitted_By, Create_Modify_Date, Severity, Status );
UPDATE `tool_tbl` SET `Release_Notes`= '$rne' WHERE id = LAST_INSERT_ID()
SQL;

$stmt = $db->prepare($sql);
$stmt->execute();
$i = 0;

do {
$i++;
}
while ($stmt->nextRowset());


$error = $stmt->errorInfo();
if ($error[0] != "00000")

{
echo "Query $i failed: " . $error[2];
}
else
{
echo "inserted Successfully";
}
die();

关于php - PHP + MySQL + PDO 中的单引号转义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28624779/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com