Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 未能将外语字符插入数据库_Php_Mysql_Character Encoding - Fatal编程技术网

Php 未能将外语字符插入数据库

Php 未能将外语字符插入数据库,php,mysql,character-encoding,Php,Mysql,Character Encoding,我正试图通过php将非英语字符(例如:Josef Kricenský)插入我的mysql数据库 这是我的密码 $name=addslashes("Josef Kricenský"); $id=1; mysql_query("'SET NAMES 'utf8'"); mysql_query("insert ignore into names (id,value) values ('$id','$name')"); 数据被插入到表中,但它只插入到“ý”,即:Josef Kricensk utf8_

我正试图通过php将非英语字符(例如:Josef Kricenský)插入我的mysql数据库

这是我的密码

$name=addslashes("Josef Kricenský");
$id=1;
mysql_query("'SET NAMES 'utf8'");
mysql_query("insert ignore into names (id,value) values ('$id','$name')");
数据被插入到表中,但它只插入到“ý”,即:Josef Kricensk

utf8_general_ci是表排序规则,MyISAM是存储引擎,这段代码以前在我的另一个站点上使用过,但似乎缺少了一些东西


如果我通过phpMyadmin插入它,它会工作,所以我猜测代码缺少一些东西,顺便说一句,php版本是5.2.7,这是一个自包含的示例,在我的机器上似乎工作得很好

<?php
// added numeric code entity to reflect actual code of OP
// $param = 'Josef Kricenský';
$param = 'Josef Kricensk&#253;';
$param = html_entity_decode($param , ENT_XML1, 'UTF-8');

// this is not how you check for utf-8 encoding
// ...except  in this simple example ;-)
// all characters but the ý are encoded in one byte, ý uses two bytes 
// -> 16 bytes -> strlen($param) should be 16
if ( 16!=strlen($param) ) { 
    die("this doesn't seem to be utf-8 encoded");
}

// the mysql_ api is deprecated. Take a look at e.g. http://docs.php.net/pdo for alternatives
$mysql = mysql_connect('localhost', 'localonly', 'localonly') or die(mysql_error());
mysql_select_db('test', $mysql) or die(mysql_error($mysql));
// SET NAMES 'utf8' doesn't tell the client library about the changed encoded
// so e.g. mysql_real_escape_string() may fail
// see http://docs.php.net/mysql_set_charset
mysql_set_charset('utf8', $mysql) or die(mysql_error($mysql));

// using a temporary table for this example...
setup($mysql);


// insert a record
echo "insert a record\n";
$query = sprintf("INSERT INTO soFoo (name) VALUES ('%s')",
    mysql_real_escape_string($param, $mysql)
);
mysql_query($query, $mysql) or die(mysql_error($mysql));

// retrieve the record
echo "retrieve the record\n";
$result = mysql_query('SELECT name FROM soFoo', $mysql) or die(mysql_error($mysql));
while( false!==($row=mysql_fetch_assoc($result)) ) {
    echo 'strlen($row[name])==', strlen($row['name']), "\n";
}

function setup($mysql) {
    mysql_query('
        CREATE TEMPORARY TABLE soFoo(
            id int auto_increment,
            name varchar(32),
            primary key(id)
        ) DEFAULT CHARSET=utf8
    ', $mysql) or die(mysql_error($mysql));
}
试着运行这个

<?php

$name = "Josef Kricenský";
$id = 1;

if( substr( $name, 14, 2 ) !== "\xC3\xBD" ) {
    trigger_error( "This file was not saved in UTF-8. Set your text editor to save the file in UTF-8" );
}

//Connect to mysql

mysql_set_charset( "utf8" );


$name_safe = "'" . mysql_real_escape_string( $name ) . "'";
$id_safe = (int)$id;

mysql_query( "insert ignore into names (id,value) values ($id_safe, $name_safe)" );

您的PHP文件也是UTF8吗?因为如果您在非unicode纯文本文件中进行编辑,则ý具有非常不同的代码。如果名称来自web,在(页面是否为UTF8格式)之前,您确定它们的编码正确吗?是否临时删除addslashes()更改了任何内容?只有当您的php页面具有正确的编码时,这才有效。根据您使用的代码编辑器的不同,大多数代码编辑器都会为您提供升级到不同编码的选项。同上。另外,除了设置名称utf8之外,我还做了一个“设置字符集utf8”-注意,我没有在utf8周围加引号-不确定这是否是一个问题,但只要看看我在代码中所做的工作就知道了,现在集合名中有一些“太多了”。可能是重复的。我试图从数据库中提取一个外来字符串,然后将其插入另一个表中,它工作得很好。但在我的实际代码中,我使用html_entity_decode()将十六进制代码转换为特殊字符,然后将其插入tablehi volkerk,感谢您的努力,朋友,试试ý;而不是ý;html_entity_decode似乎没有使用ý将其转换为相应的字符;这对我来说仍然很好。我使用的是PHP的PHP5.4.7/win32版本。nethtml_entity_decode()希望参数2是长的,字符串在“5.4.0”中给出,常量ENT_HTML401、ENT_XML1、ENT_XHTML和ENT_HTML5被添加。“您正在使用phpHi Esailija感谢代码,我尝试了它,并触发了错误。”
<?php

$name = "Josef Kricenský";
$id = 1;

if( substr( $name, 14, 2 ) !== "\xC3\xBD" ) {
    trigger_error( "This file was not saved in UTF-8. Set your text editor to save the file in UTF-8" );
}

//Connect to mysql

mysql_set_charset( "utf8" );


$name_safe = "'" . mysql_real_escape_string( $name ) . "'";
$id_safe = (int)$id;

mysql_query( "insert ignore into names (id,value) values ($id_safe, $name_safe)" );