在MySQL中存储单个空格字符

时间:2012-09-18 19:57:41

标签: php mysql unicode pdo

我编写了一个PHP脚本,用Unicode数据填充MySQL表。不过,我遇到了一些小问题。例如, space 字符(id = 32)的character列为空,即使我单独运行以下命令:

UPDATE unicode SET `character` = ' ' WHERE id = 32;

有什么想法吗?这是我用来填充表的脚本(包含的脚本common.php定义了database函数,它是一个PDO对象):

<?php

include_once('common.php');

// Fetch data from Unicode website
$file = fopen('http://www.unicode.org/Public/UNIDATA/UnicodeData.txt', 'r');

// Iterate through each line of the file
while($row = fgets($file)) {

    // Gather data
    $column = explode(';', $row);
    $id = hexdec($column[0]);
    $name = $column[1];
    $general_category = $column[2];
    $uppercase_mapping = hexdec($column[12]);
    $lowercase_mapping = hexdec($column[13]);
    $titlecase_mapping = hexdec($column[14]);

    // Build the database query
    $query = sprintf("INSERT IGNORE INTO unicode VALUES (%d, CHAR(%d USING UTF8), '%s', '%s', %d, %d, %d)",
        $id,
        $id,
        $name,
        $general_category,
        $uppercase_mapping,
        $lowercase_mapping,
        $titlecase_mapping);

    database()->query($query);
    echo $id.' ';
}

?>

1 个答案:

答案 0 :(得分:0)

character必须是CHAR

类型