【问题标题】:How insert special characters using mysql insert?如何使用mysql insert 插入特殊字符?
【发布时间】:2019-11-10 18:43:21
【问题描述】:

我是 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 中有两种类型,如下图所示”

如何替换第二种引号?

请检查附件。

 <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>

【问题讨论】:

  • 您是否考虑过使用 LOAD XML 而不是 LOAD DATA?老实说,我没有任何经验,但它似乎对 XML 更有效。
  • 我更新问题
  • 您仍然没有使用LOAD XML,这使得使用您的平面 XML 导入非常容易。

标签: php mysql xml xslt


【解决方案1】:

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

如果使用 XSLT,您可以使用 translate() 函数替换它,例如:

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

【讨论】:

    【解决方案2】:

    试试这个

    $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);
    

    【讨论】:

    • 感谢您的重播,但我更新了问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-25
    • 2014-11-17
    • 1970-01-01
    • 2012-10-17
    • 2016-04-16
    • 2019-10-13
    相关资源
    最近更新 更多