How to convert non-readable utf8 chars in MySQL to readable [duplicate] - php

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.

Related

ñ character showed as �, when retrieving from mysql to php [duplicate]

This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 4 years ago.
I have a data in my mysql database's table which is Biñan. However, when I try to retrieve it using php, it showed as Bi�an in my dropdown list.
Here is my sample code:
<?php
$query = "SELECT * from municipality";
$res = mysqli_query($conn,$query);
while($row = mysqli_fetch_assoc($res))
{
echo "<option value='".$row['m_id']."'>".$row['m_name']."</option>";
}
?>
But when I manually echo/insert the character ñ in php/mysql it displayed as is. I have also set the charset to UTF-8.
PROBLEM SOLVED!: I just have to replace the ñ with ñ in my database's table. So it shows ñ in my website.
add this in your head html file.
in a balise:
meta http-equiv="Content-Type" content="text/html; charset=UTF-8"
Try to:
ALTER DATABASE <databasename> CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
ALTER TABLE municipality CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
otherwise it's in the database.
do this in your database.
ALTER TABLE municipality COLLATE utf8_general_ci
or
ALTER TABLE municipality CONVERT TO CHARACTER SET utf8;

Chinese characters ending up blank in Database [duplicate]

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

Character issues when inserting into MySQL DB

Having an issue with strange characters showing up when inserting into a database, have tried tirelessly to figure out the issue but I am out of ideas...
Basically if I insert this data like so (this is just testing):
$valy = "…industry's favorite </em><strong><em>party of the year</em></strong><em>, </em><a href='http://www.unitingagainstlungcancer.org/getinvolved/strolling-supper-with-blues-news'><span class='s1'><em>Joan's…";
$valy = mysql_real_escape_string($valy);
$query = "INSERT INTO test_table (data) VALUES ('".$valy."')";
mysql_query($query,$dbhandle);
this will end up in the database (notice the A characters):
"...industry's favorite party of the year, http://www.unitingagainstlungcancer.org/getinvolved/strolling-supper-with-blues-news'>Joan's..."
I have tried to line up all the character settings:
php default_charset = utf-8
mysql table & row charset = utf-8
Mysql instance variables:
character set client utf8
(Global value) latin1
character set connection utf8
(Global value) latin1
character set database latin1
character set filesystem binary
character set results utf8
(Global value) latin1
character set server latin1
character set system utf8
What could this issue be?
One thing you may be missing is when you setup the connection. There you should also set the encoding to utf8.
Example:
$link = mysql_connect('localhost', 'user', 'password');
mysql_set_charset('utf8',$link);
However, don't use the mysql extension, it's deprecated: http://php.net/manual/en/function.mysql-set-charset.php
Try to set the mysql connection to UTF-8 as well:
mysql_query("SET NAMES 'utf8'");

Thai characters into UTF8 Database [duplicate]

This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 3 years ago.
I tried to put some thai sings into a utf8 (utf8_general_ci) mysql database. The data is from the facebook api. In the JSON it looks like \u0e41\u0e15\u0e07\u0e08\u0e49 (original: แตแจ้) and in the database i got some ???? (question marks).
What is the best solution to save this characters into a database?
This is my database connection with mysqli:
$DB = new mysqli('localhost', 'XXX', 'XXXXXX', 'XXXX');
$DB->query("SET CHARACTER SET 'UTF8'");
$DB->set_charset("UTF8");
ok, i found the sulotion....
the table was set to utf8_general_ci but not the utf8_general_ci table column... it was latin1....
in your db table, set your column collation to utf8_unicode_ci, then :
mysql_query("SET character_set_results=utf8");
mysql_query("SET character_set_client=utf8");
mysql_query("SET character_set_connection=utf8");

MySQL db question marks instead of hebrew characters..?

I'm trying to build a shopping cart using PHP & MySQL.
my db in MySQL is utf8 and my table in the db is utf8,
How can I use Hebrew characters?
I was able to solve this by doing the following:
the db collation has to be utf8_general_ci
the collation of the table with hebrew has to be utf8_general_ci
in your php connection script put header('Content-Type: text/html; charset=utf-8');
in xhtml head tag put <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
after selecting the db in the connection script put mysql_query("SET NAMES 'utf8'");
After a lot of work I found a solution that always works..:
without SET_NAMES.
In the conn.inc.php file, after you selected a database and connected to it, do this:
if(!mysqli_set_charset($conn, 'utf8')) {
echo 'the connection is not in utf8';
exit();
}
...and in the html always use charset utf-8;
That solved it for me. No need to use set_names(), which is ok but it annoyed the hell out of me.
You can use the PDO in your code like this:
$db = new PDO($config['DSN'], $config['dbUserName'], $config['dbPassword']);
$db->exec("SET NAMES 'utf8'");
Make sure you include the single quotation marks around the 'utf8'.
If it's an encoding problem (and it sounds like it is), this query will help:
SET NAMES utf8
Execute this query (e.g. mysql_query("SET NAMES utf8")) right after you connect and before you move any data.
More info
$conn->set_charset("utf8"); Use this for your dbconnect
Where are the question marks showing up? It may be an encoding problem somewhere other than in the data base.
To store non-ASCII characters in a database column, you need to define that column with a specific character set. You can specify a character set at 3 levels: database, table, and column. For example:
CREATE DATABASE db_name CHARACTER SET utf8
CREATE TABLE tbl_name (...) CHARACTER SET utf8
CREATE TABLE tbl_name (col_name CHAR(80) CHARACTER SET utf8, ...)
http://www.herongyang.com/PHP/Non-ASCII-MySQL-Store-Non-ASCII-Character-in-Database.html
In my case I created the DB from a dump, and this command solve it:
ALTER DATABASE <database_name> CHARACTER SET utf8 COLLATE utf8_unicode_ci;
mysql_query('SET NAMES utf8') ..... Put this in Php

Categories