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 :)
Related
I'm inserting utf-8 data to the database with this code
$conn = new mysqli($hostname, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
mysqli_set_charset($conn,"utf8");
$sql = "INSERT INTO Movies (Name, Year)
VALUES ('".$_POST["name"]."', '".$_POST["year"]."')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
But the data is added like this:
What should I do?
This is something I think you must fix in the back end, the PHP code is perfect, but you need to configure your database to utf-8 as well. You appear to be using PHPmyAdmin, when you create or import the table, you should see a drop down box to select the file's character set. Change this and you should be good to go!
That should start (end?) with Arabic 'noon' (ن), correct?
ن is the "html entity" for 'noon'.
Was the user filling in an html <form>? does it include a charset, such as <form accept-charset="UTF-8">? If not, that might be the solution.
Is the column/table declared CHARACTER SET utf8 (or utf8mb4, but not utf32)? It needs to be. See SHOW CREATE TABLE.
You can use alter command in mysql to change all the fields to utf8 format,
Please check the sql query
ALTER TABLE `Movies` CHANGE `Name` `Name` VARCHAR(40) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL;
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.
I need to use bind_param to get away with data injection. When I use bind_param, special characters such as € or ب are being saved in mysql as صذق.
1-I am sure mysql table is set up correctly.
2-I have changed the word processor's encoding to UTF8.
3-I have included many utf8 character sets all over the place.
Any thoughts how to fix this? or maybe I should start using other methods such as mysqli_real_escape_string?
($_POST["post_word"] is generated in a separate page)
$connect = mysqli_connect("r"," r","r","r"); //not real data here
$connect->set_charset('utf8');
$connect->query("SET NAMES utf8");
$querye= mysql_query("SET NAMES utf8");
mysqli_query($connect,"INSERT INTO wordtable ( wdate) VALUES (CURRENT_TIMESTAMP())");
$sqlz = "SELECT wid FROM wordtable ORDER BY wdate DESC LIMIT 1";
$resultz = mysql_query($sqlz);
$rowz = mysql_fetch_array($resultz);
$wid=$rowz['wid'];
$mysqli = new mysqli(HOSTNAME, MYSQLUSER, MYSQLPASS, MYSQLDB);
$connect->set_charset('utf8');
$unsafe_variable = $_POST["post_word"];
$stmt = $mysqli->prepare("UPDATE wordtable SET word=(?)
WHERE wid='$wid' ");
$stmt->bind_param("s", $unsafe_variable);
$stmt->execute();
$stmt->close();
$mysqli->close();
Would it be perhaps
$querye= mysql_query("SET NAMES utf8");
Your querying with MySQL here
All foreign characters such as umlauts (ü) get deleted when trying to put them into mysql.
In debugging this problem I've went over the following:
My database is UTF-8
The table in question is InnoDB utf8_general_ci. Row is longtext utf8_general_ci
I've added mysqli_set_charset($mysqli, "utf8"); right after $mysqli = new mysqli($hostname, $username, $password, $database);
It goes in using the following php:
$stmt = $mysqli->prepare("
UPDATE post
SET post = ?,
title = ?
WHERE id = ?
");
$stmt->bind_param("ssi", $clean_html, $titlePost, $id);
String before going being updated in db is
<p>SOME NEW TEXT</p><p> </p><p>üü</p>
But still nothing. The umlauts disappear. In the DB it shows up as
<p>SOME NEW TEXT</p><p></p><p></p>
What other debugging steps should I take? Thank you
Try utf8_encode() and utf8_decode() maybe it can help. before setting the value to database try
$value = utf8_encode($value);
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.