开发者

Truncation of a string in MySQL during insertion and when string contains special character 'á'

开发者 https://www.devze.com 2023-04-11 04:39 出处:网络
Why do a string truncate when a try to insert in a MySQL table? The string truncates before the character á. For instance, Málaga - Real Madrid becomes only a Min the database.

Why do a string truncate when a try to insert in a MySQL table? The string truncates before the character á. For instance, Málaga - Real Madrid becomes only a Min the database.

//***
login & select databas开发者_如何学运维e
//***
$mysqli->query('set names utf8');
$title = $mysqli->real_escape_string('Málaga - Real Madrid');
$mysqli->query("INSERT INTO article (title) VALUES ('$title')");


You can use PHP to split a string at your character á

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

?>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号