displaying Japanese characters with php from database

Multi tool use
Multi tool use


displaying Japanese characters with php from database



PHPMyAdmin is displaying Japanese characters perfectly with COLLATION =: utf8mb4_unicode_ci but it just displays ???? in the browser.



I am using the correct UTF code in the HTML headers like:


<meta content='text/html; charset=UTF-8' http-equiv='Content-Type'>
<meta charset="UTF-8">



Here are the PHP code snippets:


`<?php
//Create connection credentials
$db_host = 'localhost';
$db_name = 'quizzer';
$db_user = 'root';
$db_pass = '';

$mysqli = new mysqli ($db_host, $db_user, $db_pass, $db_name);

if($mysqli->connect_error){
printf("Connect failed: %sn", $mysqli->connect_error);
exit();
}

//Set question number
$number = (int) $_GET['n'];

/*
* Get total questions
*/
$query = "SELECT * FROM `questions`";
//Get result
$results = $mysqli->query($query) or die($mysqli->error.__LINE__);
$total = $results->num_rows;

/*
* Get Question
*/
$query = "SELECT * FROM `questions`
WHERE question_number = $number";
//Get result
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);

$question = $result->fetch_assoc();

/*
* Get Choices
*/
$query = "SELECT * FROM `choices`
WHERE question_number = $number";
//Get results
$choices = $mysqli->query($query) or die($mysqli->error.__LINE__);


<?php $question['question_number']; ?> of <?php echo $total; ?>

<?php echo $question['text']; ?>

<?php while($row = $choices->fetch_assoc()): ?>

<?php echo $row['id']; ?>" /><?php echo $row['text']; ?>

<?php endwhile; ?>`





This answer will help you, just use utf8mb4 instead of utf8 stackoverflow.com/a/202246/5527461
– Ataur Rahman
Jun 24 at 4:46





how do I add this: mysqli_set_charset('utf8') to this: $mysqli = new mysqli ($db_host, $db_user, $db_pass, $db_name);
– user2021924
Jun 28 at 22:23





2 Answers
2



(continuing from comment section)
After connecting to mysql :-


$mysqli->set_charset("utf8mb4");



This is will set your default charset to "utfmb4", so every query after this will maintain this charset.



To make this 'permanent', in my.cnf:


[mysqld]
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci





Its working perfectly now that I added: mysqli_set_charset($mysqli,"utf8"); $mysqli = new mysqli ($db_host, $db_user, $db_pass, $db_name); mysqli_set_charset($mysqli,"utf8");
– user2021924
Jul 1 at 22:30




Just add mysqli_set_charset($mysqli,"utf8");




$mysqli = new mysqli ($db_host, $db_user, $db_pass, $db_name);

mysqli_set_charset($mysqli,"utf8");






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

6dvRRIGVq bsSc,37jpGeswU 0O4JUZVSd04pbNYLO45,66gRjA7JQYqBscHblwaJo6gDp4wjYJ
tz70VGT,xNx,4Z9dx1PlAJ3Adajr,aSoXcHILW52VW1Gt4,uzcgOmQqd 0sK,9gOdy3PVe5 3PSpgb sn oM n,c1H5DiG K

Popular posts from this blog

Rothschild family

Boo (programming language)