UTF-8 encoding not working properly php - php

When I try to print a string from a DB the spanish accent is turned into ? .
Example de f?tbol, salones para actividades. ?Para m?s informaci?n
I have tried following things
added
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
Then I tried using utf8_encode() and utf8_decode() none of these things have worked.
I checked int he database the string is properly saved but when I print on the browser it is not printing properly.
Also in my Mysql DB I have mysql_set_charset('utf8',$dbSelect); added.
I think it is some kind of MySql error .
Update1
This is happening only when I do query using Join operation
SELECT xxxx1.tag,xxxx1.registro,xxxx1.valor FROM campofeed ".
"INNER JOIN registrofeed ON xxx.id = xxxx1.registro ".
"WHERE xxxx.feed='xxx' and xxxx.id='xxxxx'"

Try setting the mysql_set_charset, i.e.:
$link = mysql_connect('localhost', 'user', 'password');
mysql_set_charset('utf8',$link);
//the rest of the code...

Try this header in first line of php:
header('Content-type: text/html; charset=utf-8');

You can check the encode type of your file. Save as UTF-8. I always check on notepad++.

Related

sql server +PHP select records which has non utf8 characters not working

I have an old application which uses mssql_query() for accessing database results from SQL SERVER.The issue I face now is for not utf8 usernames. For example ,username is usüß.When I check in sql server its giving me the results.But in PHP page,it is giving me empty results.
SELECT userID FROM user_table where username='usüß';
I know that mssql_query() is deprecated,but this is an old system. I have tried the following.
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
I have used
ini_set('mssql.charset', 'UTF-8');
Nothing is working for me.Can anyone help me please
Have you tried utf8 decode?
$name = utf8_decode('usüß');
$sql = "SELECT userID FROM user_table where username='$name'";
try this:
$utf8 = 'usüß'; // file must be UTF-8 encoded
$iso88591_1 = utf8_decode($utf8);
var_dump($iso88591_1);
$iso88591 = 'usüß'; // file must be ISO-8859-1 encoded
$utf8_1 = utf8_encode($iso88591);
var_dump($utf8_1);
Have you tried SET NAMES?
It would be something like:
<?php
mssql_query("SET NAMES 'utf8'");
//your query
?>

"â€" character in front and at the end of my string in the MySQL database

-I've made a select input with options,
-I've given each option a value="something".
The problem is that in the database the submited value appears like "â€somethingâ€", instead of "something".
I've made research and I've putted :
$link= mysql_connect(DB_HOST, DB_USER , DB_PASS);
mysql_set_charset('UTF8',$link);
& already had:
<meta charset="utf-8" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
In my database the collation is of type:"utf8_unicode_ci" for all columns
What is the problem then ?
Most likely the data in your database was not written there in UTF8. So you are getting a mismatch of encodings.
Relevant SO question here: "’" showing on page instead of " ' "
My bad,
I copied from Microsoft Word(because there is where I wrote the code ONLY TO ORDER IT ASCENDING BY LETTERS) plese don't BOOOOO!!!
So the code was like:
<option value=”something”>something</option>
the answer is:
"something"!=”something” ; " != ”
That was the problem :D

How to read a Persian string from MySql in PHP?

I inserted persian string to my table rows in my database, where my columns collection is set to utf8mb4_persian_ci.
But when i want read data from MySql database to php , it doesn't show persian string correctly and just shows ? ? ? ?
I read many article for fix this problem but it didn't work for me
I used this code after connecting to database
$mysqli = new mysqli(HOST, USER, PASSWORD, DATABASE);
$mysqli->set_charset("utf8");
And this code at header of php codes
header("Content-Type: text/html;charset=UTF-8");
And this code at top of html codes
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
But it don't work and I can't see persian string in php pages where it's read from MySql to php
How to fix this problem?
This is the code I used to get this to work properly, since I used all your code, this means your mysql database configuration is incorrect, please recreate your database with the right collation, make sure the table collation is also the same and the field type to be TEXT
Here is the code:
<?php
$mysqli = new mysqli('localhost', 'root', '', 'persian');
$mysqli->set_charset("utf8");
header("Content-Type: text/html;charset=UTF-8");
$mysqli->query("INSERT INTO user SET name='فارسی / پارسی'");
$result = $mysqli->query("SELECT * FROM user");
$result = $result->fetch_object();
echo '<pre dir="ltr">';var_dump($result);echo '</pre>';
echo $result->name;?>
$mysqli->set_charset("utf8");
header("Content-Type: text/html;charset=UTF-8");
instead of $mysqli->set_charset("utf8"); try
$mysqli->set_charset("windows-1256");

UTF-8 and German characters?

I have problem with German characters on my web site,
in html/php part of website i have this code to set utf-8:
<meta charset="utf-8">
in mysql, i have this code to set utf-8
SET CHARSET 'utf8';
Here is some word on German: Gemäß
Here is how that word looks in mysql table:
Gemäß
Here is how that word is shown on the site: Gemäß
What is a problem? Thanks.
I was using this code to get title:
$title = mysql_real_escape_string(htmlentities($_POST['title']));
I just override that to
$title = $_POST['title'];
At first, make sure, that you have UTF-8 characters in your database.
After that, try using SET NAMES 'UTF8' after connecting to MySQL:
$con=mysqli_connect("host", "user", "pw", "db");
if (!$con)
{
die('Failed to connect to mySQL: ' .mysqli_connect_errno());
}
mysqli_query($con, "SET NAMES 'UTF8'") or die("ERROR: ". mysqli_error($con));
As the manual says:
SET NAMES indicates what character set the client will use to send SQL
statements to the server... It also specifies the character set that the server should
use for sending results back to the client.
Try SET NAMES 'utf8' or SET NAMES 'utf-8'. Some of these works fine for portuguese, probably for german too. I just can't remember which one is correct, but if it is not, an error will be produced.
you should make sure that the CONNECTION is also utf-8.
with mysqli this is done with something like this:
$connection = mysqli_connect($host, $user, $pass, $db_name);
$connection->set_charset("utf8");
Now if somehow you ended up with wrong characters in the database there is a way to make it right:
in a PHP script, retrieve the information as you do now, i.e without setting the connection. This way the mistake will be inverted and corrected and in your php file you will have the characters in the correct utf-8 format.
in a PHP script, write back the information with setting the connection to utf-8
at this point you should see the character correct in your database
now change all your read/write functions of your site to use the utf-8 from now on
in HTML5 use
<meta charset="utf-8">
in HTML 4.0.1 use
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
the results are html entity encoded as if they were processed by htmlentities(), I wonder if your variables are ibserted as received from the form or are being processed by say a wysiwg editor for instance?
Anyway, these should print fine on an html template but an html_entity_decode() should do it to.
Hope this helps
Set the data type in your database to use UTF-8 as well, this should solve the problem.
I had the same problem. which I solved by using:
if you have already created your table, you need the modify the character set as:
alter table <table name> convert to character set utf8 collate utf8_general_ci.
your tables character set is set to latin_swedish by default by MySQL.
also, you might face some problems while retrieving the data and displaying it to you page.For that include: mysql_set_charset('utf8') just below the line where you have connected your database.
eg:
mysql_connect('localhost','root','');
mysql_select_db('my db');
mysql_set_charset('utf8');
You will need to do this for php 5.x
$yourNiceLookingString =
htmlspecialchars ($YourStringFromDB, ENT_COMPAT | ENT_HTML401, 'ISO-8859-1');
and for php 4.x
$yourNiceLookingString = htmlspecialchars($YourStringFromDB);

How to display Unicode data with PHP [duplicate]

This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 2 years ago.
table 'abc' data :
tid title
1 வெள்ளிக்கிழமை ஐ.
2 கோலாகல தொடக்க
$sql=mysql_query("select title from abd where tid='1'");
$row=mysql_fetch_array($sql);
$title = $row['title'];
echo $title;
OutPut displaying like this:
????????????????
But I want to display
வெள்ளிக்கிழமை ஐ.
Solution
<?php
mysql_query ("set character_set_results='utf8'");
$sql=mysql_query("select title from abd where tid='1'");
$row=mysql_fetch_array($sql);
$title = $row['title'];
echo $title;
?>
Try to set charachter encoding after mysql_connect function like this:
mysql_query ("set character_set_client='utf8'");
mysql_query ("set character_set_results='utf8'");
mysql_query ("set collation_connection='utf8_general_ci'");
For new version of PHP which used mysqli, use this code after connect to mysql:
mysqli_set_charset($link, 'utf8');
Try to make sure the browser recognizes the page as Unicode.
Generally, this can be done by having your server send the right Content-type HTTP header, that includes the charset you're using.
For instance, something like this should work :
header('Content-type: text/html; charset=UTF-8');
echo "வெள்ளிக்கிழமை ஐ";
If this works, and your dynamically generated page still doesn't :
make sure your data in your MySQL database is in UTF-8 too
this can be set for each table, or even columns, in MySQL
and make sure you are connecting to it using UTF-8.
Basically, all your application should use the same encoding :
PHP files,
Database
HTTP server
execute query
SET NAMES 'utf8'
right after connecting to database
First you need to convert your mysql data format to utf-8, see this great article by oreilly:
.
Turning MySQL data to UTF-8
.
After that make sure that your page's encoding type is utf-8:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php header('Content-type: text/html; charset=UTF-8');echo "<font face='Bamini' size='5'>" . "m.Nkhfd; Fkhh;" ."<br />";
echo "<font face='Bamini' size='10'>" . "m.Nkhfd; Fkhh;" ."<br />";
?>
or
<?php
header('Content-type: text/html; charset=UTF-8');
echo "வெளà¯à®³à®¿à®•à¯à®•à®¿à®´à®®à¯ˆ à®";
?>
While inserting the data into a MySQL database using the relevant collation (ex. utf8_general_ci for Unicode characters, i.e Hindi). Also in use the PHP predefined function utf8_decode to display the characters in HTML.

Categories