2011-10-05 56 views
-1

爲什麼嘗試在MySQL表中插入字符串時會截斷字符串?該字符串在字符á之前截斷。例如,Málaga - Real Madrid在數據庫中只成爲M在插入過程中在MySQL中截斷字符串並在字符串中包含特殊字符'á'

//*** 
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')"); 
+0

分割字符串你確定那個'了'是UTF-8'了'? –

+0

你確定標題字段的字符集是UTF-8嗎? – hakre

+0

不,我不確定。我從另一個網站獲取數據。我認爲它雖然不是UTF-8。我可以以某種方式將其轉換爲UTF-8? – einstein

回答

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')"); 

?> 
相關問題