How to read a Persian string from MySql in PHP? - 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");

Related

PHP not displaying some UTF-8 characters correctly

PHP is retriving data from MySQL database. Now it's suppose to display the results to screen, but since my database contains characters like (ä,ö,ü,õ), which PHP displays (�,䄔). Database displays all characters correctly, so I don't think the problem is there.
I add following line to my code, that sets charset to UTF-8.
$conn->set_charset("utf8");
After adding that line, PHP displays following characters „”.
Also tried:
$conn->query("SET character_set_results=utf8");
$conn->query("SET NAMES 'utf8'");
How can I get PHP to display correct characters?
Here is the important part of my code
$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "SELECT name FROM employee";
$stmt = $conn->prepare($sql);
$stmt->execute();
$stmt->bind_result($employee);
while($stmt->fetch()){
echo "$employee|";
}
$stmt->close();
$conn->close();
I'm running on PHP 7 and MySQL 10.1.19-MariaDB.
Consider the following things in this situations.
Make sure the header match the following
Content-Type:text/html; charset=UTF-8
and try setting UTF-8 in html
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
The source file encoding can make problems when string are hard coded in file. Since this file echoing it that seem to be no issue.

UTF-8 encoding not working properly 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++.

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);

UTF8 reading Persian string as ? in MySQL 5.0

I have a table in MySQL 5.0, which I put city names in it in Persian and using a page I try to read a specific city name!
It used to word, but suddenly from today, the city names in my page are all '?????' like!
I go to the phpMyAdmin, change all the collation settings to "utf_persian_ci" and nothing happens!
The interesting part of it is that the "Browse" option of phpMyAdmin shows everything ok (all city names are ok!) but when I try to get them using this kind of query from a page the thing happens:
$result = dbquery("SELECT * FROM ".DB_CITIES." WHERE cty_company_id = ".$_GET['cmp']." AND EXISTS (SELECT cu_cmp_id FROM ".DB_COMPANY_USERS." WHERE cu_cmp_id = ".$_GET['cmp']." AND cu_usr_id = $user_id) AND EXISTS (SELECT ctus_user_id FROM scada_city_users WHERE ctus_user_id = $user_id AND ctus_city_id = cty_id)");
Thanks in advance!
There is a simple way to tell the database it should deliver UTF-8 encoded strings. Just tell your connection object, which character-set you expect, the database does the rest for you.
$db = new mysqli($dbHost, $dbUser, $dbPw, $dbName);
// tell the db to deliver UTF-8 encoded strings.
$db->set_charset("utf8");
The collation only defines how two entries should be compared, it's not the same as defining the charset. Your HTML page shoud also be UTF-8 encoded, some more informations you can find here here.

PDO database for arabic characters

I want to insert Arabic characters to database using pdo with php, but I got these characters in database
الجامع &#1575
and when I use
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
the result was
??????????
I make the collection of the columns in phpMyadmin
utf8mb4_unicode_ci
and this
utf_unicode_ci
but I still have the same error.
And I tried to make the connection like this: (totti,totti is not my really username and password)
parent::__construct('mysql:host=localhost;dbname=ams-competation;charset=utf8"', 'totti', 'totti');
but still I get the same error
And I also tried like this:
parent::__construct('mysql:host=localhost;dbname=ams-competation;charset=utf8', 'totti', 'totti'
,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
and doesn't work :(
and after submitting the form and before inserting to database i echo the values and i got arabic characters , that means my problems is in database , but echo works just without using meta
and when i tried to insert arabic characters from phpmyadmin IT WORKS
i am using
phpmyadmin in XAMPP 1.7.4
php 5.5.3
finally i found the solution and it is:
make the collection of the columns utf8_unicode_ci
put <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> in the header
Try setting the connection to utf-8 when making connection to the database.
If you're following rules and best practices:
Set the constants first in your config file like this:
define('DB_SERVER', 'your_hostname');
define('DB_USERNAME', 'your_username');
define('DB_PASSWORD', 'your_password');
define('DB_CHARSET', 'UTF-8');
define('DB_DATABASE', 'your_dbname');
define('PDO_DSN', 'mysql:host=' . DB_SERVER . ';dbname=' . DB_DATABASE . ';charset=' . DB_CHARSET);
Then set your connection like this:
$_dbhandler = new PDO(PDO_DSN, DB_USERNAME, DB_PASSWORD,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES UTF8"));
Quick and dirty
Set your connection like this:
$dbhandler = new PDO('mysql:host=your_hostname;dbname=your_dbname;charset=UTF-8', $user, $pass);
Your problem is most likely not in how you access the database. The characters you see are HTML encoded, so it looks like you're using something like htmlentities() on your data before writing it to the DB.
define('DB_CHARSET', 'UTF-8'); not working
use define('DB_CHARSET', 'UTF8'); instead

Categories