在插入期间以及当字符串包含特殊字符'á'时在MySQL中截断字符串

时间:2011-10-05 18:41:18

标签: php mysql sql mysqli special-characters

为什么在尝试插入MySQL表时字符串会截断?字符串在字符á之前截断。例如,Málaga - Real Madrid在数据库中只变为M

//***
login & select database
//***
$mysqli->query('set names utf8');
$title = $mysqli->real_escape_string('Málaga - Real Madrid');
$mysqli->query("INSERT INTO article (title) VALUES ('$title')");

1 个答案:

答案 0 :(得分:0)

您可以使用PHP在角色上分割字符串á

<?php
// Put the data in to a string
$string = 'Málaga - Real Madrid';

// Split it by the character
$split_string = explode('á', $string);

//***
login & select database
***/
$mysqli->query('set names utf8');

// Put the first split in to the real_escape function
$title = $mysqli->real_escape_string($split_string[0]);

// Run the query
$mysqli->query("INSERT INTO article (title) VALUES ('$title')");

?>
相关问题