I have a question about converting charset from inside mysql query.
I have a 2 databases. One for the website (joomla), the other for forum (IPB).
I am doing query from inside joomla, which by default have "SET NAMES UTF8".
I want to query a table inside the forum databases. A table called "ibf_topics". This table has latin1 encoding.
I do the following to select anything from the not-utf8 table.
//convert connection to handle latin1.
$query = "SET NAMES latin1";
$db->setQuery($query);
$db->query();
$query = "select id, title from other_database.ibf_topics";
$db->setQuery($query);
$db->query();
//read result into an array.
//return connection to handle UTF8.
$query = "SET NAMES UTF8";
$db->setQuery($query);
$db->query();
After that, when I want to use the selected tile, I use the following:
echo iconv("CP1256", "UTF-8", $topic['title'])
The question is, is there anyway to avoid all this hassle?
For now, I can't change forum database to UTF8 and I can't change joomla database to latin1 :S
you could open 2 database connections, 1 to joomla and one to IPB. you'll be using more resources, but it will make your code easier to read, and you can do all the configuration (charset etc) in the db setup. this will also make it easier to migrate later, in case your situation changes - you won't need to go through your code removing all the SET NAMES.
$query = "SET NAMES UTF8";
$db->setQuery($query);
$db->query();
$query = "select id, title from other_database.ibf_topics";
$db->setQuery($query);
$db->query();
/*
you can set charset after database connection
*/
header('Content-Type: text/html; charset=utf-8');
/*
after connection
*/
mysql_query("SET NAMES utf8");
/*
or
*/
mysql_query("SET CHARACTER SET utf8");
/*
or
*/
mysql_query("SET COLLATION_CONNECTION = 'utf8_general_ci'");
Related
mysql_query("SET NAMES 'utf8'");
mysql_query('SET CHARACTER SET utf8');
$sql = "SELECT * FROM mytable";
$data = selectArray($sql);
insert arabic data is = `تظهر البيانات العربي
display in my database = تظهر البيانات العربيØ
display in cakePhp = تظهر البيانات العربية (display proper)
display in corePhp = تظهر البيانات العربيØ
Let me know any one have solution of this issue.
when create table use like this
CREATE DATABASE mydb DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;
Mojibake -- search this forum for that. You hit a common problem; this is a duplicate.
I am developing a local website which gives information in local language. In my MySQL database , there is a table called pageContent and it has a column called "text". that "text" column type is text and collation is utf8_sinhala_ci
In MySQL it displays the fonts normally:
But when I take it in to PHP page and echo the value, then it shows question marks like ??????????.
But if I hard code something and echo, then it displays normally in PHP page also.
I feel something goes wrong when I take values from the database. But I couldn’t found the error.
I have tried following codes also.
mysql_query ("set collation_connection='utf8_sinhala_ci'");
$result = mysqli_query($this->connecDB,"SELECT * FROM pageContent");
But that doesn’t work.
I feel something goes wrong when I take values from the database. But
i couldn't found the error. I have tried following codes also.
You need to make sure your entire chain from the connection, to the database, to the tables is all UTF8 clean. I have a detailed answer to a similar question here.
But in your case, check the actual MySQL server my.cnf file. The following would set the whole chain to UTF-8:
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysqld]
collation-server = utf8_unicode_ci
init-connect='SET NAMES utf8'
character-set-server = utf8
More immediately, look at your code:
mysql_query("set collation_connection='utf8_sinhala_ci'");
$result = mysqli_query($this->connecDB,"SELECT * FROM pageContent");
In one line you are calling mysql_query and the next you are calling mysqli_query. But these are conflicting methods that should not be used together. It should simply be mysqli_query like so:
mysqli_query($this->connecDB, "SET collation_connection='utf8_sinhala_ci'");
$result = mysqli_query($this->connecDB,"SELECT * FROM pageContent");
Note how I am setting mysqli_query($this->connecDB,… before your SET collation_connection='utf8_sinhala_ci'. That will send the query on the same connection you are using for $result. Or perhaps you can try this instead:
mysqli_query($this->connecDB, "SET NAMES 'utf8'");
$result = mysqli_query($this->connecDB, "SELECT * FROM pageContent");
Make sure that your PHP-file it self is using the same encoding as your database (UTF8). That have caused me errors in the passed.
If that doesn't help, an ugly solution would be to convert the output with utf8_decode().
So in my case, I had tried changing the collation from utf8mb4_unicode_ci for mysql and had to change it to uft8_general_ci.
Then pasted :
mysqli_set_charset( $con, 'utf8');
right before I did the SELECT command.
This is my code for reading from db :
/*
$DB_SERVER="db_server_name";
$DB_USER_READER="root";
$DB_PASS_READER="passw*rd";
$DB_NAME="db_name";
$DB_PORT="port number";
$SELECT_WHAT="`name_of_column_as_in_your_table`";
$WHICH_TBL="`table_name`";
$ON_WHAT_CONDITION="`id`='7'";
*/
$con = mysqli_connect($DB_SERVER, $DB_USER_READER, $DB_PASS_READER, $DB_NAME, $DB_PORT);//this is the unique connection for the selection
mysqli_set_charset( $con, 'utf8');
$slct_stmnt = "SELECT ".$SELECT_WHAT." FROM ".$WHICH_TBL." WHERE ".$ON_WHAT_CONDITION;
$slct_query = mysqli_query($con, $slct_stmnt);
if ($slct_query==true) {
//Do your stuff here . . .
}
And it worked like a charm. All the best. The above code can work with reading chineese, russian or arabic or any international language from the mysql database table column holding such data.
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've made a simple PHP page to get the POST data and fetch a sql query then print the result. I'm using the mysql_fetch_array function.
The code works great but the response is a non-Unicode text, and it returns something like this:
?????ABC?????
note that the database collation is UTF8 and data stored are shown correctly in phpMyAdmin. I even used this META tag in php page but it results the same:
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">
Any idea?!
Add these lines of code before the first query:
mysql_query("SET NAMES 'utf8'");
mysql_query("SET CHARACTER SET utf8");
mysql_query("SET SESSION collation_connection = 'utf8_unicode_ci'");
Or you can edit your mysql configuration to use utf8 by default. Check here for what you need to do.
Change MySQL default character set to UTF-8 in my.cnf?
UPDATE
The function mysql_query is deprecated, so mysqli object can be used like so:
$mysqli = new mysqli("localhost", "MYSQL_USER", "MYSQL_PASS", "MYSQL_DB");
$mysqli->query("SET NAMES 'utf8'");
$mysqli->query("SET CHARACTER SET utf8");
$mysqli->query("SET SESSION collation_connection = 'utf8_unicode_ci'");
mysql_set_charset('utf8', $link);
Where $link is a connection created with mysql_connect
Try using mb_internal_encoding("UTF-8"); for details http://php.net/manual/en/function.mb-internal-encoding.php
I have a database where the table is encoded as utf8. I have a value in it that's in Korean. The characters display fine in the database. But when they are echoed from the database I get a bunch of question marks.
Here is my code, after the connect and select_db statements:
mysql_query('SET NAMES utf8');
$query = 'SELECT * FROM english WHERE id = ' . $_GET['dealerID'];
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result);
What am I doing wrong here? (Yes, 'english' is the right table). I tried Michael's suggestion below to encode the table as utf8_general_ci and I get a MySQL error. Suggestions? What's the correct name of the character set?
If I run a query in PHPMyAdmin, I get 서울시 서초구 서초1동 1425-10 세중프라자 4층/
Review this guide which talks about UTF8, mysql, and PHP all together.
Summary:
make sure the browser knows the page is in utf8
<?php header("Content-type: text/html; charset=utf-8"); ?>
The table in mysql needs to be set for utf8 as well as the string fields within the db
utf8_general_ci
tell php that when it talks to mysql to talk in utf8
mysqli_set_charset('utf8');
Here are some more details as well.