使用PHP将中文字符插入mysql数据库

时间:2013-04-13 12:45:56

标签: php mysql insert

我想问为什么下面显示的代码无法将中文字符$split 5插入MySQL数据库。我已经使用db和表创建了utf8_general_ci

 $mysqli = new mysqli($GLOBALS["mysql_host"], $GLOBALS["mysql_user"], $GLOBALS["mysql_passwd"], $GLOBALS["mysql_database"]);
$stmt = mysql_query("SET character_set_results 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8");
$stmt = $mysqli->prepare("INSERT INTO cell (`add`) VALUES (?)");
$stmt->bind_param("s", $split[5]);
$stmt->execute();
$stmt->close();
$mysqli->close();

1 个答案:

答案 0 :(得分:0)

首先,有一个错字。您已使用mysql_query()代替mysqli_query()


遵循PHP中的mysqli文档,mysqli_set_cahrset()是可行的方法:

$mysqli->set_charset ('utf8');

所以我会在上面添加一行并删除mysql_query('SET ...');语句