i have a mysql database that stores the arabic in it, it works fine in both savng the arabic characters and retrieving the content, the probem i am receiving is when i retrieve this arabic content from the database and want to use in another query it does not work:
//Database Connection
mysql_query("SET CHARACTER SET utf8");
$sql = mysql_query("SELECT * FROM `categories` WHERE 1");
while($row1 = mysql_fetch_array($sql))
{
$category = $row1['label_id']; //Arabic Content is retrieved displayed correctly
$query1 = mysql_query("SELECT * FROM `user_list` WHERE `Category` = '" .$category . "' ORDER BY `private_number` DESC");
$query1 = mysql_query("SET CHARACTER SET utf8");
while($row = mysql_fetch_array($query1))
{
//returns zero rows
}
}
but when i copy the string echo above and query without concatenating it does work very well
example:
$query1 = mysql_query("SELECT * FROM `user_list` WHERE `Category` = 'الأفراد' ORDER BY `private_number` DESC");
What is wrong with my php code and what can i do to change it
Make sure that both Database and Tables Collation is set to utf8_general_ci. if not, use a tool like this to convert collation after backing up the database.
The encoding of the files should be UTF-8 as well.
Also, check that Meta tag content-type is set to utf-8:
<meta charset="utf-8"> for HTML5, OR
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> for XHTML.
Related
This question already has answers here:
The ultimate emoji encoding scheme
(2 answers)
Closed 6 years ago.
I am having problems decoding UTF8 characters in my script from my sql.
Lets say I have two characters coming from mysql:
'á' & ❤️
with my script á is decoded fine however, the emoticon is decoded in â¤ï
What am I doing wrong?
$conn = new mysqli($servername, $username, $password, $dbname);
$sql="SELECT * FROM `community` ORDER BY `community`.`date` DESC LIMIT 25";
mysqli_set_charset($conn, "utf8"); //UTF8
$result = $conn->query($sql);
while($row = $result->fetch_assoc()) {
$comment = $row['comment'];
echo $comment . "</br>";
//echo htmlentities($comment); not working... white screen
}
UPDATE
I have changed Database and Tables
$conn = new mysqli($servername, $username, $password, $dbname);
$sql="SELECT * FROM `community` ORDER BY `community`.`date` DESC LIMIT 25";
mysqli_set_charset($conn, "utf8"); //UTF8
$result = $conn->query($sql);
while($row = $result->fetch_assoc()) {
$comment = $row['comment'];
$comment = mb_convert_encoding($comment, "UTF-8");
echo $comment . "</br>";
//echo htmlentities($comment); not working... white screen
}
Your issue here is probably with the database.
You need to set your database charset to UTF-8 in order to query it correctly, what you are doing is to get a string and setting Client side default charset using
mysqli_set_charset($conn, "utf8"); //UTF8
That's not enough, so I would recommend you to run an SQL query like
ALTER DATABASE databasename CHARACTER SET utf8 COLLATE utf8_unicode_ci;
in order to update it. If what you need is to change a single table use
ALTER TABLE tablename CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;
Finally you can check it using
SELECT DEFAULT_COLLATION_NAME FROM data_dictionary.SCHEMAS WHERE SCHEMA_NAME = 'databasename' LIMIT 1;
As an extra appointment and just in case in PHP you can convert the encoding of a string using
$comment = mb_convert_encoding($comment, "UTF-8"); //Change encoding
echo mb_detect_encoding($str, "auto"); // Check encoding
Of course you should make a backup before making any of these changes, just in case.
EDIT: The proper order to run these queries, is:
Run it in the whole DB using the first query
Run it table by table using the second query
Check if the charset has been set correctly using the third query
EDIT 2 : Remember to set the tag <meta charset="UTF-8"> in your html file.
I hope this helps you :)
I am fetching data with mysql_fetch_assoc() the string is Arabic it's echoing like this ?????
I tried encoding but not worked.
$dbc = mysql_connect("localhost", "root", "", "tedmon") or die("Error 3: " . mysql_error());
mysql_query("SET NAMES utf8");
mysql_query("SET character-set utf8");
$sqlForStudent = "SELECT `MONITORING_PROVINCE`,count(*) FROM `student_interview` group by `MONITORING_PROVINCE`";
$sqlForadmin = "SELECT `pro`,count(*) FROM `ttc_admin_interview` group by `pro`";
$sqlForteacherev = "SELECT `pro`,count(*) FROM `ttc_teachers_evaluation` group by `pro`";
$sqlForteacherint = "SELECT `pro`,count(*) FROM `ttc_teachers_interview` group by `pro`";
$studentset = mysql_query($sqlForStudent);
$adminset = mysql_query($sqlForadmin);
$teacherevset = mysql_query($sqlForteacherev);
$teacherintset = mysql_query($sqlForteacherint);
while($teacherevres = mysql_fetch_array($teacherevset)){
echo $teacherevres['pro'];
}
Change from
mysql_query("SET NAMES utf8");
mysql_query("SET character-set utf8");
To
mysql_query("SET NAMES 'utf8'");
mysql_query("SET CHARACTER SET utf8");
Make sure you have added charset in head your page.
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Note mysql extension is deprecated as of PHP 5.5.0, and has been removed as of PHP 7.0.0
how can i get MYSQL output in right encoding? I have this escaped string in MYSQL DB: Wannahëal, i need Wannahëal.
Thank you!
// Connect
$link = mysql_connect('xxx', 'xxx', 'xxx') or die("Can't connect to MYSQL DB");
mysql_set_charset("UTF8", $link);
//Choose a DB
mysql_select_db('xxx') or die("Can't select MYSQL DB");
$query = "SELECT * FROM mr_characters";
$result = mysql_query($query) or die('Query failed!');
header('Content-type: text/html; charset=utf-8');
while($row = mysql_fetch_object($result))
{
echo $row->title . "<br>";
}
You can use mysql_set_charset to set your client character set.
You need to set the charset in your charset, I think in your case utf8.
Either you can make this in the creating of your db:
CREATE DATABASE myDB DEFAULT CHARACTER SET utf8;
[...]
Or in the tables:
[...]
CREATE TABLE myTable DEFAULT CHARACTER SET utf8;
(
[...]
EDIT: If you don't set the charset before you make the data input into your MySQL server, your MySQL server isn't able to read the data correctly and saves them incorrectly. So not your code is incorrect nor your server had an any failure, quite honestly your server saved your data incorrect.
I'm trying to SELECT elements from a database. This database uses the UTF-8 encoding.
For example, in one column named title, I have a row with this text: Crăciunul. It appears fine in phpMyAdmin as Crăciunul.
Now, I'm trying to connect to database in PHP and SELECT this database element. The problem is that it appears as Cr?ciunul on my website, which has the UTF-8 encoding setted, too.
This problem appears only if I get elements form database. I have made a string in PHP = Crăciunul, and it is shown fine on the website.
Thanks a lot!
EDIT 1:
$con = mysql_connect("HOST","USER","PASS");
mysql_select_db("DB", $con);
if (mysql_errno($con) !== 0)
{
die("Failed to connect to MySQL: " . mysql_error());
}
if (!mysql_select_db("DB"))
{
die('Could not select database: ' . mysql_error());
}
$query = mysql_query("SELECT * FROM table LIMIT 1");
while($row = mysql_fetch_array($query)) {
echo $row['title'];
}
Set the character encoding of the DB connection.
$con = mysql_connect("HOST","USER","PASS");
mysql_set_charset('utf8', $con);
Also, please don't use this deprecated mysql API for new development.
try this ...
place this code before executing query ...
mysqli_set_charset ($dbconnection, "utf8");
try this
mysql_query('SET NAMES UTF8');
and
mysqli_set_charset($conn,"UTF8");
I have a table that data was inserted in it some time ago. data included by php and character set in it was utf-8 .
mysql_query("set names utf8");
now ,I need this table in another project which all data will show on php mysql default character set.
problem : my data is persian, when I set charachter set utf-8, every things is ok, but without character set data convert to the "?????" what should I do?!
I want to import all data form old table to new table on new character set !!
Try this might help you
<?php
header('Content-Type: text/html; charset=utf-8');
?>
and then in the connection
<?php
$dbLink = mysql_connect($argHost, $argUsername, $argPassword);
mysql_query("SET character_set_results=utf8", $dbLink);
mb_language('uni');
mb_internal_encoding('UTF-8');
mysql_select_db($argDB, $dbLink);
mysql_query("set names 'utf8'",$dbLink);
?>
///
$SERVER = "http://localhost:8084";
$db = mysql_connect('localhost','root') or die("Not Connected");
$sldb = mysql_select_db('vifarbydata',$db);
mysql_query("set names utf8");
$sql = "SELECT * FROM `region` ";
$res = mysql_query($sql);
while($h=(mysql_fetch_array($res)))
{
$row[] = $h;
}
mysql_close($db);
/// set another character set
$SERVER = "http://localhost:8084";
$db = mysql_connect('localhost','root') or die("Not Connected");
$sldb = mysql_select_db('vifarbydata',$db);
foreach ($row as $item)
{
echo $sql = "insert into `region2` (id,name,myId) values (".$item['id'].",'".$item['name']."','".$item['myId']."') ";
mysql_query($sql);
}
#afsane, using mysqldump, you can dump the data from old_table and then import it into the new_table which has the new character encoding.
mysqldump -u user -p --no-create-info schema old_table > old_table.dump
mysql -u user -p schema new_table < old_table.dump
This will be much faster than doing a conversion via PHP.