简体   繁体   English

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

[英]How insert special characters using mysql insert?

I am new in MYSQL, I have a XML file of hotels, that include HotelCode and Description of hotel. 我是MYSQL的新手,我有旅馆的XML文件,其中包括旅馆代码和旅馆描述。 Xml file like below 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>

Also i using below sql query to insert xml data to database 我也使用下面的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')

            ;");

But here data's are not insert to hotels table. 但是这里的数据没有插入到酒店表中。 because description includes some special characters like ',"", etc. 因为说明中包含一些特殊字符,例如',“”等。

There is any way some thing like mysqli_real_escape_string 有什么办法像mysqli_real_escape_string

update: "But now i identify that quotes comes in the xml two types like below picture" 更新: “但是现在我确定引号来自xml中的两种类型,如下图所示” 在此处输入图片说明

How to replace the second type of quotes? 如何替换第二种类型的引号?

please check the attached file. 请检查附件。

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

The 2nd quote character is the RIGHT SINGLE QUOTATION MARK ( &rsquo; &#8217; U+2019 ). 第二个引号字符是正确的单引号( &rsquo; &#8217; U+2019 )。

If using XSLT, you can replace it using the translate() function, eg: 如果使用XSLT,则可以使用translate()函数替换它,例如:

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

Try this 尝试这个

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

        ;");

It will open your file and add slashes at the single quotes and double quotes. 它将打开您的文件,并在单引号和双引号处添加斜杠。

When You retrieve table data Use like this to prevent slashes to be print 检索表数据时像这样使用以防止斜线被打印

echo stripslashes($str);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM