【问题标题】:Insert special characters in the database with a bulk query使用批量查询在数据库中插入特殊字符
【发布时间】:2014-08-13 18:24:27
【问题描述】:

我需要在数据库中插入大量具有相应信息的国家/地区。这可行,但是特殊字符以 ü 和 é 的形式插入到数据库中,例如。如何在数据库中插入带有特殊字符的国家(最好使用下面的代码)?

以下是我为此使用的代码。我使用多查询,因为我运行了多个查询(下面的代码只是一小部分)。举个例子,我列出了一些带有特殊字符的国家(实际列表大约是 250 个国家)。

插入_countries.php:

include 'mysql_connect.php';

// select database
    mysqli_select_db( $mysqli, 'my_database' );

// check connection
if ( $mysqli->connect_errno > 0 ) {
    trigger_error( 'Database connection failed: '  . $mysqli->connect_error, E_USER_ERROR );
}

// sql query
$sql = 'INSERT INTO countries (country_code_num, country_code_iso, country_en, country_nl, country_de, country_fr, country_continent, country_region, country_union) VALUES
        ("016", "AS", "American Samoa", "Amerikaans-Samoa", "Amerikanisch-Samoa", "Samoa américaine", "Oceania", "Polynesia", ""),
        ("020", "AD", "Andorra", "Andorra", "Andorra (Fürstentum)", "Andorre (Principauté)", "Europa", "Southern Europe ", ""),
        ("044", "BS", "Bahamas", "Bahama\'s", "Bahamas", "Bahamas", "Americas", "Caribbean", ""),
        ("894", "ZM", "Zambia", "Zambia", "Sambia", "Zambie", "Africa", "Eastern Africa", "");';

// Execute multi query
if ( $mysqli->multi_query( $sql ) ) {
    do {
        if ( $res = $mysqli->store_result() ) {
            while ( $row = $res->fetch_row() ) {
                printf( "%s\n", $row[0] );
            }
            $result->free();
        }
        if ( $mysqli->more_results() ) {
            printf( "-----------------\n" );
        }
    }
    while ( $mysqli->next_result() );
}

$mysqli->close();

编辑:

根据要求(一部分)创建表语句以及创建数据库。

// create database
$sql = "CREATE DATABASE IF NOT EXISTS my_database CHARACTER SET utf8 COLLATE utf8_general_ci";

// create table
$sql = "CREATE TABLE IF NOT EXISTS countries ( 
        country_code_num INT(3) NOT NULL,
        country_code_iso VARCHAR(3) NOT NULL,
        country_en VARCHAR(75) NOT NULL,
        country_nl VARCHAR(75) NOT NULL,
        country_de VARCHAR(75) NOT NULL,
        country_fr VARCHAR(75) NOT NULL,
        country_continent VARCHAR(30) NOT NULL,
        country_region VARCHAR(30) NOT NULL,
        country_union VARCHAR(30) NOT NULL,
        PRIMARY KEY ( country_code_num ));";

【问题讨论】:

  • 视情况而定。您是否一直使用 UTF-8?你用mysqli::set_charset吗?请也发布您的 CREATE TABLE 语句。
  • 我想我一直使用 UTF-8。虽然没有使用过 mysqli::set_charset。
  • 拨打mysqli::get_charset 电话并查看结果。请注意 mysqli::set_charset 的注释: 这是更改字符集的首选方式。不推荐使用mysqli_query()来设置(如SET NAMES utf8)。
  • 解决方案确实是设置一个字符集。我在带有国家代码的 sql 查询上方添加了$mysqli->set_charset('utf8');,现在它们已正确插入数据库中。谢谢!

标签: php mysql database mysqli special-characters


【解决方案1】:

cmets 中讨论的结果是字符集没有正确设置。插入

 $mysqli->set_charset('utf8');

解决了这个问题。这是为连接设置所需字符集的首选方法,我将文档引用到mysqli::set_charset

注意:

这是更改字符集的首选方法。使用 不推荐使用mysqli_query()来设置(如SET NAMES utf8)。

【讨论】:

    【解决方案2】:

    确保您在整个 PHP 和数据库交互过程中使用相同的字符集。必须检查几件事:

    1. 选择合适的字符集。在这种情况下,我将假设 UTF8。
    2. 将您的 PHP 文件保存为 UTF8。
    3. 打开连接后立即向您的 PHP 中添加查询 SET NAMES 'utf8'
    4. Set your table and column char sets to UTF8。至少在表定义中使用CHARSET=utf8

    【讨论】:

    • 不要使用SET NAMES,而是使用mysqli_set_charset
    • 糟糕,我不小心删除了自己的评论。我在我的 sql 查询上方设置了$mysqli->set_charset('utf8');,它可以工作:)。
    【解决方案3】:

    感谢 VMai 和 Gumbo,问题得以解决。

    解决方案是设置字符集。我在带有国家代码的 sql ($sql) 查询上方添加了$mysqli->set_charset('utf8');,现在它们已正确插入数据库中。谢谢!

    【讨论】:

      猜你喜欢
      • 2016-02-18
      • 1970-01-01
      • 2013-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-24
      • 2020-08-30
      • 2018-02-26
      相关资源
      最近更新 更多