Latin characters not working in html5 using UTF-8 [duplicate] - php

This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 6 years ago.
Hey so i'm getting text with latin characters like "ó" from a mysql database. This is the connection along with the query i'm using to get the data, placing it in an array:
<?php
header('Content-Type: text/html; charset=UTF-8');
$con=new mysqli ("localhost","root","","chroniclemark");
$sqlget = "SELECT descricao FROM items;";
$sqldata = mysqli_query($con,$sqlget) or die ('error getting database');
while ($row = mysqli_fetch_array($sqldata, MYSQLI_ASSOC)) {
$lol5[] = $row['descricao'] ;
}?>
I'm then trying to display the the text outside of the <?php ?> statement like so:
<p ><?php echo $lol5[0]; ?></p>
All of the text is fine except for the "ç", "ó", "ã" and so on.
I also have a meta tag <meta charset="UTF-8"> in my header but it doen't help.
In the database, the data is set to "utf8mb4_unicode_ci".
What can i do to fix this? Any idea?

#Fred -ii Thanks for pointing me in the right direction in the comments.
All i had to do was use mysqli_set_charset($con, 'utf8mb4'); before making the query
$sqlget = "SELECT descricao FROM items;";
$sqldata = mysqli_query($con,$sqlget) or die ('error getting database');
It's all fine now :)

Related

Showing utf8_general_ci data (non-eng) on php [duplicate]

This question already has answers here:
UTF-8 all the way through
(13 answers)
Non English characters appear as question marks on my php page - appear fine in database
(2 answers)
Closed 7 years ago.
I have saved my data on MYSQL database using "utf8_general_ci". I wrote it on Bangla font.
But when I am trying to show data using php it is showing "????..." instead of showing the charecters.
<?php
$con = mysqli_connect("localhost","appsaadr_edu","pass","appsaadr_edu");
$result = mysqli_query($con,"SELECT * FROM lnews ORDER BY id DESC");
echo "{\"news\":";
$arr = array();
while($rows = mysqli_fetch_array($result)){
$test['id'] = $rows['id'];
$test['title'] = $rows['title'];
$test['text'] = $rows['text'];
$test['time'] = $rows['time'];
array_push($arr,$test);
}
echo json_encode($arr);
echo "}";
?>
What to do now?
Make sure the file is encoded in UTF-8 without BOM
Add : mysqli_set_charset($con,"utf8"); between the lines 3 and 5
Put all the $test['XXX'] like this utf8_encode($test['XXX'])
Make sure to add the data into the database also through utf8_encode before

I can't get Chinese characters from MYSQL to PHP

I found a lot of answers for my problem but nothing worked.
I have Chinese characters in my MYSQL database and when I SELECT my field in php I get '???' instead of '我们的产品'. I was able to INSERT this characters from php to my MYSQL database.
With characters like 'éà' I have no problem to get them in PHP.
My database, table and field are encoded in utf8_unicode_ci. There is my php file(also encoded in utf-8) :
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<?php
$query = mysql_query("SELECT * FROM table");
$arr = mysql_fetch_array($query))
echo ($arr['field']);
?>
If I replace my code like this it prints 我们的产品 :
$query = mysql_query("SELECT '我们的产品'");
arr = mysql_fetch_array($query)
echo ($arr[0]);
I hope I gave enough information.
set charset as utf8 before making a mysql query, like this
mysql_connect('localhost','mysql_user','your_password');
mysql_select_db('my_db') or die( "Unable to select database");
mysql_set_charset('UTF8');
mysql_query('Your query here');

PHP MYSQL encoding issue ( Turkish Characters )

core.php
$con=mysqli_connect("localhost","root","","mmogezgini");
header('Content-Type: text/html; charset=utf-8');
gamelisti.php
$result = mysqli_query($con,"SELECT * FROM games");
while($row = mysqli_fetch_array($result))
{
?>
HTML CODES
<?php echo $row['game_name']; ?>
HTML CODES
<?php }
mysqli_close($con);
?>
i use turkish characters like "ö,ç,ğ,ı" i see these correctly in database but when i select them from database and show with php echo they looks like question mark=(?)
my encoding on database utf8_general_ci
Double check your HTML encoding.
http://php.net/manual/en/mysqli.set-charset.php (just after you connect to DB: mysqli_set_charset($con, 'utf8');
Are you using prepared statements? (not related but important).

Arabic not supported in mysql and php [duplicate]

This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 9 years ago.
I have this part of code that works fine only the arabic issue.
$result = mysql_query("SELECT login, password, name, role FROM qm_users WHERE login = '$login'");
if (!$result) {
die('Invalid query: ' . mysql_error());
}
$row = mysql_fetch_array($result);
echo $row["name"];
The name row contains arabic name.
The output of echo is ???? ???? ????
In my myphpadmin of xamp server, here the structure
Check below possible solution,
1) Your table structure 'Collation' must be 'utf8_unicode_ci'.
2) Also try to set below thing in php file
ini_set('default_charset','utf-8');
mysql_set_charset('utf8');
header('Content-type: text/html; charset=utf-8');
If you are going to save UTF8 encodings to your MySQL database first set your connection encoding to UTF8.
<?php
$connection = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
mysqli_set_charset($connection, "utf8");
?>

reading utf-8 content from mysql table [duplicate]

This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 12 months ago.
I have a mysql table with contents
the structure is here:
I want to read and print the content of this table to html
This is my code:
<?php
include("config.php");
$global_dbh = mysql_connect($hostname, $username, $password)
or die("Could not connect to database");
mysql_select_db($db)
or die("Could not select database");
function display_db_query($query_string, $connection, $header_bool, $table_params) {
// perform the database query
$result_id = mysql_query($query_string, $connection)
or die("display_db_query:" . mysql_error());
// find out the number of columns in result
$column_count = mysql_num_fields($result_id)
or die("display_db_query:" . mysql_error());
// Here the table attributes from the $table_params variable are added
print("<TABLE $table_params >\n");
// optionally print a bold header at top of table
if($header_bool) {
print("<TR>");
for($column_num = 0; $column_num < $column_count; $column_num++) {
$field_name = mysql_field_name($result_id, $column_num);
print("<TH>$field_name</TH>");
}
print("</TR>\n");
}
// print the body of the table
while($row = mysql_fetch_row($result_id)) {
print("<TR ALIGN=LEFT VALIGN=TOP>");
for($column_num = 0; $column_num < $column_count; $column_num++) {
print("<TD>$row[$column_num]</TD>\n");
}
print("</TR>\n");
}
print("</TABLE>\n");
}
function display_db_table($tablename, $connection, $header_bool, $table_params) {
$query_string = "SELECT * FROM $tablename";
display_db_query($query_string, $connection,
$header_bool, $table_params);
}
?>
<HTML><HEAD><TITLE>Displaying a MySQL table</TITLE></HEAD>
<BODY>
<TABLE><TR><TD>
<?php
//In this example the table name to be displayed is static, but it could be taken from a form
$table = "submits";
display_db_table($table, $global_dbh,
TRUE, "border='2'");
?>
</TD></TR></TABLE></BODY></HTML>
but I get ???????? as the results:
Where is my mistake?
Four good steps to always get correctly encoded UTF-8 text:
1) Run this query before any other query:
mysql_query("set names 'utf8'");
2) Add this to your HTML head:
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
3) Add this at top of your PHP code:
header("Content-Type: text/html;charset=UTF-8");
4) Save your file with UTF-8 without BOM encoding using Notepad++ or any other good text-editor / IDE.
Set the charset as utf8 as follows:
$conn = new mysqli($servername, $username, $password, $dbname);
$conn->set_charset("utf8");
You are not defining your HTML page as UTF-8. See this question on ways to do that.
You may also need to set your database connection explicitly to UTF8. Doing a
mysql_query("SET NAMES utf8;");
^
Put it right under your database connection script or include and MAKE sure you have it placed before you do any necessary queries. Also, for collocation please take the time to make sure your
setting it for your proper syntax type and general_ci seems working good for me when used. As a finale, clear your cache after banging your head, set your browser to proper encoding toolbar->view->encoding
Setting the connection to UTF8 after establishing the connection takes care of the problem. Don't do this if the first step already works.
UTF-8 content from MySQL table with PDO
To correctly get latin characters and so on from a MySQL table with PDO,
there is an hidden info coming from a "User Contributed Note" in the PHP manual website
(the crazy thing is that originally, that contribution was downvoted, now luckily turned to positive .. sometime some people need to got blamed)
my credits credits go to this article that pulled the solution and probably made that "User Contributed Note" to turn positive
If you want to have a clean database connection with correct Unicode characters
$this->dbh = new PDO(
"mysql:host=".DB_HOST.";dbname=".DB_NAME.";charset=utf8",
DB_USER,
DB_PASS);
try this :
mysql_set_charset('utf8', $yourConnection);
Old ways have been deprecated. If you are using PHP > 5.0.5 and using mysqli the new syntax is now:
$connection->set_charset("utf8")
Where $connection is a reference to your connection to the DB.
I tried several solutions but the only one that worked
is that of Hari Dass:
$conn->set_charset("utf8");

Categories