Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm echoing into my page some accented words, that sometimes are printed like this:

acatl??n de ju??rez

And after refreshing the page a few times again, it prints them like this:

acatlán de juárez

And the process repeats back and forth. This is not something that should be happening in my application because I'm comparing many things with this words.

I already have utf-8 on my header

<meta charset="UTF-8">

The database collation used: utf8_unicode_ci

The variable causing this errors is assigned like this:

$theString = "acatlan de juarez";

$a = array(
    'animal' => array('acatlán de juárez','hów','áre'),
    'color'  => array('yóu','good','thanks')
);
$b = array(
    'animal' => array('acatlan de juarez','how','are'),
    'color'  => array('you','good','thanks')
);
$theString = str_replace($b['animal'], $a['animal'], $theString);

// $theString now is $theString = "Acatlán de Juárez"

So $theString variable, when echoed or doing a query, sometimes prints "acatl??n de ju??rez" and some other times prints "acatlán de juárez".


EDIT

I'm starting to receive alot of answers regarding database queries, it actually has nothing to do with the Database, accents are correctly ouputed from the database. Problem is with the variable $theString explained in the code above, it is not printed like it should, with accents.


EDIT 2

I don't know what's happening now, I'm really confused, in this example:

$a = array(
'animal' => array('acatlán de juárez','hów','áre'),
'color'  => array('yóu','good','thanks')
);
echo $a['animal'][0];

My code outputs the word "acatlán de juárez" perfectly.

But the I have my array $cities (exactly like my array $a) but with states and cities. Like this:

$cities = array(
'state1' => array('acatlán de juárez','city1','city2'),
'state2'  => array('city1','city2','city3')
);
echo $cities['state1'][0];

And now "acatlán de juárez" becomes "acatl??n de ju??rez".... WHY?!?


EDIT 3

Question solved, it was another function bugging the charset format, thank you everyone.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
115 views
Welcome To Ask or Share your Answers For Others

1 Answer

I had exactly the same issue as yours a few weeks ago and the solved the problem after modifying database COLLATE and CHARSET as well as how I handle MySQL queries. My post is here.

  1. Set you DB's "default collection" to utf8mb4 - utf8mb4_unicode_ci
  2. Make sure "COLLATE" property of table fields are set to utf8mb4_unicode_ci as well.
  3. Set "meta" tag to <meta charset="utf-8"> in your HTML.
  4. When you do SELECT, INSERT, UPDATE on MySQL, call $mysqli->set_charset('utf8mb4');

Note: I suggest you to read up on why utf8mb4 or why not utf8 in general. Also read this.

EXAMPLE TABLE

CREATE TABLE `quote` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `code` char(4) COLLATE utf8mb4_unicode_ci NOT NULL',
  `quote` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

EXAMPLE SELECT

Note: Pay attention to set_charset()

$mysqli->set_charset('utf8mb4');
$result = $mysqli->query('SELECT * FROM quote');
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
   //Do something with $row
}

EXAMPLE INSERT/UPDATE

$mysqli->set_charset('utf8mb4');
$sql = "INSERT INTO quote (code, quote) VALUES (?, ?)";
//$sql = "UPDATE ......";
$stmt = $mysqli->prepare($sql);
$stmt->bind_param('ss', $_POST['code'], $_POST['quote']);
if (! $stmt->execute()) {
    echo $stmt->error;
}
echo 'Nice one';

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...