I tried my best. Still I can't store emojis in the database.
Here is a code which i implemeted.
But the database doesn't support emojis
Please anyone can give me a solution for this?
In databse data stored as ??????
column alter
ALTER TABLE custom_comments CHANGE "c1" "c1" TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL ;
database alter to utf8mb4_unicode_ci
table alter
ALTER TABLE `custom_comments` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
header file header('Content-type: text/html; charset=utf-8');
sqlquery
mysqli_query ($con,"set character_set_client='utf8'");
mysqli_query ($con,"set character_set_results='utf8'");
mysqli_query ($con,"set collation_connection='utf8_general_ci'");
$c1=$_REQUEST['c1'];
$sql_update1 = "INSERT INTO custom_comments (c1) VALUES('".$c1."')";
mysqli_query($con,$sql_update1)or die(mysqli_error($con));
data stored in database as
?ghhhgggggh
??☺
connection
$con = mysqli_connect("localhost","insta_bot","insta_bot","insta_bot");
$con->set_charset('utf8mb4');
In your connection code you are correctly using
$con->set_charset('utf8mb4');
but then in your sqlquery section you change it to utf8 when it should be utf8mb4:
mysqli_query ($con,"set character_set_client='utf8mb4'");
mysqli_query ($con,"set character_set_results='utf8mb4'");
mysqli_query ($con,"set collation_connection='utf8mb4_unicode_ci'");
The MySQL docs describe how these system variables affect the handling of characters.
Related
This question already has answers here:
strange character encoding of stored data , old script is showing them fine new one doesn't
(2 answers)
How to convert an entire MySQL database characterset and collation to UTF-8?
(20 answers)
Closed 7 years ago.
I have Arabic text stored in mysql like this format
شقق جديده غبر مسكونه 220 متر..
.150متر..طبربور ..ااسكانات قموم
والنجار للاستÙسار
it is in this format because I didn't use the below before inserting data into database from HTML textboxes:
mysql_query('SET CHARACTER SET utf8');
My question now, how to convert the text into MySQL columns to readable text like this:
شقق جديده غبر مسكونه 220 متر ..150متر..طبربور ..ااسكانات قموم والنجار للاستفسار
I tried the below for one column cell to check, but it didn't work:
mysql_query('SET CHARACTER SET utf8');
$sql = 'SELECT content FROM messages WHERE id=500';
$qry = mysql_query($sql);
$result =mysql_fetch_object($qry);
$text= $result->content;
mysql_query('SET CHARACTER SET utf8');
$sql ='UPDATE messages SET content= "'.$text.'" where id=500';
mysql_query($sql);
Note: if I use the below
mysql_query('SET CHARACTER SET utf8');
before I insert data into database from HTML textarea, then it works fine for store and read, but I need to convert the already entered data into tables.
Thanks.
The solution in brief can be done in 3 simple steps:
A. Change the Database collation to utf8 via the command below:
ALTER DATABASE <db_name> CHARACTER SET utf8 COLLATE utf8_general_ci;
already the table cells I want to change have collation of utf8_general_ci
B. Implement this query to change the content:
update messages set content=CONVERT(BINARY CONVERT(content USING latin1) USING utf8);
C. add the below before you connect to the DB:
mysql_query('SET CHARACTER SET utf8');
And you are done!!! Thanks for all
Use the ALTER DATABASE and ALTER TABLE commands.
ALTER DATABASE databasename CHARACTER SET utf8 COLLATE utf8_unicode_ci;
ALTER TABLE tablename CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;
Source.
This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 3 years ago.
Im making an API for a company that inserts orders via this api into our DB, but I'm running into a problem:
Chinese characters don't get stored into my tables, how to fix this?
I have:
- My test file which uses cURL to post set to utf8 via header*
- The API code which inserts the data set to utf8 via header*
- Stored my phpfile as an UTF-8 encoded file
- mysqli connection set to utf8 ($connection->set_charset("utf8"))
- mysql-database set to utf8_general_ci
- mysql-table set to utf8_general_ci
- mysql-columns set to utf8_general_ci
- I insert the values into a VARCHAR and into a TEXT column (for test purposes)
- Inserted the values directly into the query -> no result
- Inserted the values via iconv("ISO-8859-1","UTF-8", $text)-> weird result
- Inserted the values via iconv("UTF-8","UTF-8", $text)-> no result
- Tested characters like áéíúó, which work perfectly fine
- Set my postvalues for curl via foreach($post as $k=>$v){ $data.= $k.'='.$v;} to send as data
- Set my postvalues for curl via http_build_query($post) to send as data
- Used a query before inserted doing SET CHARACTER SET utf8
- Used a query before inserted doing SET NAMES 'utf8'
When I echo the query before inserting it, I see the chinese characters
When I check the database, it's blank values
(not unexpected:) When I Select it and output it, it's blank
*PHP charset header:
header('Content-Type: text/html; charset=utf-8');
I'm out of ideas, anyone?
I have same problem and use this query and my problem solved
("SET CHARACTER SET utf8")
$mysqli->query("SET CHARACTER SET utf8")
Verify that the field has utf8_unicode_ci
CREATE TABLE `table` (
`id` int(15) NOT NULL,
`fieldname` varchar(20) NOT NULL,
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
and the charset is utf8
mysqli_set_charset($con,"utf8");
OOP
public function open_connection() {
$this->connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
if(mysqli_connect_errno()) {
$msg = "Database connection failed: ";
$msg .= mysqli_connect_error();
$msg .= " (" . mysqli_connect_errno() . ")";
exit($msg);
}
// Change character set to utf8
mysqli_set_charset($this->connection,"utf8");
}
I can't insert Arabic language to Mysql database using the below code. When I check the values using phpMyAdmin, the field would be empty. Why is that so?
$query = "INSERT INTO table (name) VALUES ('عماد')";
mysql_query($query);
name field should be utf8
in the same time use this query directly after connecting to mysql, before executing any other query like the insert statement in your example
set names 'utf8'
then check phpmyadmin output, it should be correct
Use this line .... u can insert special charecters.
mysql_real_escape_string($query);
/**
* ALTER TABLE `myTable` CHARACTER SET utf8 COLLATE utf8_general_ci;
*
* There’s a generic way of doing by simply executing the MySQL query:
* SET NAMES utf8;
*
* ...and depending on which client/driver you’re using
* there are helper functions to do this more easily instead:
*/
// With the built in mysql functions
mysql_set_charset('utf8');
// With MySQLi
$mysqli->set_charset("utf8")
// With PDO_MySQL (as you connect)
$pdo = new PDO(
'mysql:host=localhost;dbname=test',
'username',
'password',
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")
);
Make sure the field's name collation is set to: 'utf8_unicode_ci' in PHPMyAdmin
Goto you database and change your field's Collation value to utf8_unicode_ci .Thanks me later
for Java tuts visit: Introduction to Java
I have a field "scroller" with collation "utf8_unicode_ci" in mysql database.
I am trying to save an arabic text to this field.
When i insert it to the mysql directly it works.
Using this code below i tried to edit the field. When i echo it it displays correctly. But when it is saved using update its saving like "??????? : 89 ????? ?? ?????? AMRI ????????? ?????? ???? ??????? ????? ????? ???????" in the database.
echo "<meta http-equiv=Content-Type content=text/html; charset=UTF-8>";
require 'config.php';
mysql_query("SET NAMES 'utf8'");
mysql_query('SET CHARACTER SET utf8');
$scroller=htmlentities($_POST['scroller'],ENT_QUOTES,"UTF-8");
mb_internal_encoding("UTF-8");
echo $scroller;
$sql="UPDATE arab_scroller SET scroller='$scroller' WHERE page='$id1' ";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
header( 'Location:index.php' );
Please help me out
I just tested same thing with Turkish. If your table's default collation is not UTF8, you might have these kind of problems.
Please try this first:
ALTER TABLE tbl_name DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
And then try to run your code again.
Also, it is better to use mysql_set_charset instead of SET NAMES:
http://php.net/manual/en/function.mysql-set-charset.php
You just change table Collation utf8_general_ci by default it takes latin;
OR
you run this query :
ALTER TABLE tbl_name DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
This is really works with me.
When I'm trying to insert cyrillic text into MySQL database it inserts it like:
г???????????? ?? ????????
Рісѓрїр°ріс‹рї р° с‹рір°рї
So, I have two pages: registration.php and addUser.php. In each of them
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
Database consist of 11 tables, each table has collation: utf8_general_ci, type: MyISAM. Each field in every table has Collation: utf8_general_ci.
When I'm writing to database directly in phpMyAdmin and then show this data to web-page. In English and Russian - all OK.
But when I'm full my form with personal data on registration.php and then going to addUser.php - all cyrillic characters displayed like I wrote upper - on page and in database too.
function AddNewUser($Name, $Surname, $FatherName, $Email, $Password, $Phone, $DegreeID, $RankID,
$Organization, $Department, $Country, $City, $Address, $Job)
{
//fetch data from database for dropdown lists
//connect to db or die)
$db = mysql_connect($GLOBALS["gl_kussdbName"], $GLOBALS["gl_kussUserName"], $GLOBALS["gl_kussPassword"] ) or die ("Unable to connect");
//to prevenr ????? symbols in unicode - utf-8 coding
mysql_query("SET NAMES 'UTF8'");
//select database
mysql_select_db($GLOBALS["gl_kussDatabase"], $db);
$sql = "INSERT INTO UserDetails (
UserFirstName,
UserLastName,
UserFatherName,
UserEmail,
UserPassword,
UserPhone,
UserAcadDegreeID,
UserAcadRankID,
UserOrganization,
UserDepartment,
UserCountry,
UserCity,
UserAddress,
UserPosition)
VALUES(
'".$Name."',
'".$Surname."',
'".$FatherName."',
'".$Email."',
'".$Password."',
'".$Phone."',
'".$DegreeID."',
'".$RankID."',
'".$Organization."',
'".$Department."',
'".$Country."',
'".$City."',
'".$Address."',
'".$Job."'
);";
//execute SQL-query
$result = mysql_query($sql, $db);
if (!$result)
{
die('Invalid query: ' . mysql_error());
}
//close database = very inportant
mysql_close($db);
}
?>
There also such information in phpMyAdmin:
auto increment increment 1
auto increment offset 1
autocommit ON
automatic sp privileges ON
back log 50
basedir \usr\local\mysql-5.1\
big tables OFF
binlog cache size 32,768
binlog format STATEMENT
bulk insert buffer size 8,388,608
character set client utf8
(Global value) cp1251
character set connection utf8
(Global value) cp1251
character set database cp1251
character set filesystem binary
character set results utf8
(Global value) cp1251
character set server cp1251
character set system utf8
character sets dir \usr\local\mysql-5.1\share\charsets\
collation connection utf8_general_ci
(Global value) cp1251_general_ci
collation database cp1251_general_ci
collation server cp1251_general_ci
completion type 0
concurrent insert 1
So I need to properly show, save and select russian text from database. Thanx!
connect timeout 10
datadir \usr\local\mysql-5.1\data\
Try calling mysql_set_charset('utf8'); after connecting to the database. I think it's similar to executing a SET NAMES query, but since the PHP manual says using that function over a SET NAMES query is recommended, I'd try it.
Also, when you display your content, you could try echo htmlentities($string, ENT_COMPAT, 'UTF-8');
I store greek in tables created like ths:
CREATE TABLE `test` (
`test_id` SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
`test_name` VARCHAR(30) NOT NULL,
PRIMARY KEY (`test_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT;
Or if table already created I guess you can change the charset it in the phpmyadmin interface. Maybe this helps.
Check your MySQL configuration and ensure that your encoding is defined correctly.
Add these lines to my.cnf or my.ini, which ever your installation uses.
These settings did the trick for me:
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysqld]
character-set-server=utf8
I have tried multiple collations in phpMyAdmin as well as changing the charset of the page, which didn;t make sense but i was willing to try anything after two days of research. this command helped me: mysql_set_charset('utf8');
Collation on the column was set to koi8r_general_ci
Edit your structure field to set collation utf16_general_ci.
After that insert your data.