如何使用mysql insert插入特殊字符?

时间:2019-06-28 20:57:35

标签: php mysql xml xslt

我是MYSQL的新手,我有一个旅馆的XML文件,其中包括旅馆代码和旅馆描述。如下所示的Xml文件

<hotels>
        <hotel>
        <hotelcode>1</hotelcode>
        <description>San cassiano residenzia D’epocaVenice’s Grand Canal.Overview Situated overlooking Venice’s Grand Canal, San Cassiano Residenzia D’Epoca is a historic hotel with plenty of charm</description>
        </hotel>
<hotel>
        <hotelcode>2</hotelcode>
        <description>San cassiano residenzia D’epocaVenice’s Grand Canal.Overview Situated overlooking Venice’s Grand Canal, San Cassiano Residenzia D’Epoca is a historic hotel with plenty of charm</description>
        </hotel>
<hotel>
        <hotelcode>3</hotelcode>
        <description>San cassiano residenzia D’epocaVenice’s Grand Canal.Overview Situated overlooking Venice’s Grand Canal, San Cassiano Residenzia D’Epoca is a historic hotel with plenty of charm</description>
        </hotel>
    <hotels>

我也使用下面的sql查询将xml数据插入数据库

$conn_1->query("LOAD DATA LOCAL INFILE '".$rs_file_path."'
            INTO TABLE hotels
            CHARACTER SET utf8
            LINES STARTING BY '<hotel>' TERMINATED BY '</hotel>'
            (@tmp)
            SET
                hotelcode = ExtractValue(@tmp, 'hotelcode'),
                description= ExtractValue(@tmp, 'description')

            ;");

但是这里的数据没有插入到酒店表中。因为说明中包含一些特殊字符,例如',“”等。

通过任何方式,mysqli_real_escape_string

更新: “但是现在我确定引号来自xml中的两种类型,如下图所示” enter image description here

如何替换第二种引号?

请检查附件。

 <hotels>
            <hotel>
            <hotelcode>1</hotelcode>
            <description>Located near S'Arenal Venice’s yacht club</description>
            </hotel>
<hotel>
            <hotelcode>2</hotelcode>
            <description>Located near S'Arenal Venice’s yacht club</description>
            </hotel>
<hotel>
            <hotelcode>3</hotelcode>
            <description>Located near S'Arenal Venice’s yacht club</description>
            </hotel>
</hotels>

2 个答案:

答案 0 :(得分:0)

第二个引号字符是正确的单引号(&rsquo; &#8217; U+2019)。

如果使用XSLT,则可以使用translate()函数将其替换,例如:

<xsl:value-of select='translate(description, "&#8217;", "&apos;")'/>

答案 1 :(得分:-1)

尝试一下

$content = file($rs_file_path);
$filedata = str_replace("'", "\'", $content);
$filedata = str_replace('"', '\"', $content);
$conn_1->query("LOAD DATA LOCAL INFILE '".$rs_file_path."'
        INTO TABLE hotels
        CHARACTER SET utf8
        LINES STARTING BY '<hotel>' TERMINATED BY '</hotel>'
        (@tmp)
        SET
            hotelcode = ExtractValue(@tmp, 'hotelcode'),
            description= ExtractValue(@tmp, 'description')

        ;");

它将打开文件,并在单引号和双引号中添加斜杠。

检索表数据时像这样使用以防止斜杠被打印

echo stripslashes($str);