I have a table in MySQL 5.0, which I put city names in it in Persian and using a page I try to read a specific city name!
It used to word, but suddenly from today, the city names in my page are all '?????' like!
I go to the phpMyAdmin, change all the collation settings to "utf_persian_ci" and nothing happens!
The interesting part of it is that the "Browse" option of phpMyAdmin shows everything ok (all city names are ok!) but when I try to get them using this kind of query from a page the thing happens:
$result = dbquery("SELECT * FROM ".DB_CITIES." WHERE cty_company_id = ".$_GET['cmp']." AND EXISTS (SELECT cu_cmp_id FROM ".DB_COMPANY_USERS." WHERE cu_cmp_id = ".$_GET['cmp']." AND cu_usr_id = $user_id) AND EXISTS (SELECT ctus_user_id FROM scada_city_users WHERE ctus_user_id = $user_id AND ctus_city_id = cty_id)");
Thanks in advance!
There is a simple way to tell the database it should deliver UTF-8 encoded strings. Just tell your connection object, which character-set you expect, the database does the rest for you.
$db = new mysqli($dbHost, $dbUser, $dbPw, $dbName);
// tell the db to deliver UTF-8 encoded strings.
$db->set_charset("utf8");
The collation only defines how two entries should be compared, it's not the same as defining the charset. Your HTML page shoud also be UTF-8 encoded, some more informations you can find here here.
Related
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 have a database that contains both chinese characters and english. when i queried the database to display them on my browser using php, all i get for the chinese characters are gibberish, none readable characters.
what i have tried:
ini_set('mssql.charset', 'UTF-8')
on the client side i made sure i included the meta tag specifying UTF-8.
am using mssql server
language is php
any help will be great. thanks
I worked on a Chinese migration project last year and can point you at a few things to look at:
Check you are using NVARCHAR and NCHAR as your DB data types. Do not use VARCHAR/CHAR
I've not used PHP much, but in the majority of languages I've worked in, you need to make sure the appropriate locale is set/supported.
So a few things to check, try outputing some Chinese text on a sample .php file to confirm it is displaying as expected. Next, update your schema with the correct data types. Finally, point your test script at the DB to see if the data is displayed as expected.
Try setting the character set within the PHP file after you've connected. Then run the query.
<?php
include('db_access.php');
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
mysqli_set_charset($dbc, "utf8"); // Set the character set here!!
$query = "SELECT chinese FROM my_table";
while ($row = mysqli_fetch_array($query)) {
echo $row['chinese'].'<br />';
}
?>
PHP Manual: http://cn2.php.net/manual/zh/mysqli.set-charset.php
I have set up a table in phpMyAdmin. I haven't changed the charsets or anything. I inserted a text in a new row, and when I try to SELECT that row and output it with PHP, the letters ÆØÅ are displayed as ���, however if I try to edit the field in phpMyAdmin, the letters are displayed correctly. What do I do wrong that phpMyAdmin does correctly?
If your PHP file is already UTF-8 encoded, you should tell your database, that you need UTF-8. Instead of fiddling with the configurations of MySQL, just tell your connection object, which character-set you expect, the database does the rest for you.
This is an example for a mysqli connection object:
$db = new mysqli($dbHost, $dbUser, $dbPw, $dbName);
$db->set_charset("utf8");
Afterwards your queries will return UTF-8 encoded results.
SET NAMES 'charset_name'
http://dev.mysql.com/doc/refman/5.0/en/charset-connection.html
First you will want to determine if it is the browser charset that is wrong, or mysql.
Try swapping the charset in your browser to utf8 or if it is already to iso-8859.
If that doesn't fix it try changing the charset in your query by doing
SET CHARACTER SET charsetname;
what's up? :-)
I have one problem and i hope you can help me with it.
One friend of mine have a simple solid html website and i implemented little php; CRUD system for articles... problem i came across is placing and getting cyrillic characters from mysql database.
What i want to achive is next:
In the main navigation there are some separated sections, whose names, ids and item's order i want to place in mysql and than to pull names and to put each name as a link. Names are supposed to be cyrillic characters.
The problem comes when i, using php mysql_fetch_assoc function, try to display names which are inserted with cyrillic characters in database row, collation of row is utf8_general_ci, and i end with ????? insted of original characters. If i submit cyrillic characters via submit form to mysql it shows something like this У.
How can i solve this, thanks in advance!? :-)
Make sure you call this after connecting to database.
mysql_query("SET NAMES UTF8");
Also make sure that HTML file has charset meta tag set to UTF-8 or send header before output.
header("Content-Type: text/html; charset=utf-8");
I had the same problem until I encoded the 'Collation' column in my table to 'utf8_bin'.
if its really mysql fetch assoc messing up you should try:
mysql-set-charset
from the docs:
Note:
This is the preferred way to change
the charset. Using mysql_query() to
execute SET NAMES .. is not
recommended.
also make sure your files are saved as utf8 and check iconv_set_encoding / iconv_get_encoding
For anyone having more complex issues with legacy project upgrades from versions before PHP 5.6 and MYSQL 5.1 to PHP 7 & Latest MySQL/Percona/MariaDB etc...
If the project uses utf8_encode($value) you can either try removing the function from the value being prepared and use the accepted answer for setting UTF-8 encoding for all input.
--- OR ---
Try replacing utf8_encode($value) with mb_convert_encoding($value, 'utf-8')
PDO USERS
If you are using PDO here are two ways how to set utf8:
$options = [
\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'
];
new \PDO($dsn, $username, $passwd, $options);
--- OR ---
$dsn = 'mysql:host=localhost;charset=utf8;'
new \PDO($dsn, $username, $passwd);
I can confirm that mb_convert_encoding($value, 'utf-8') to SQL table using utf8_unicode_ci works for Cyrillic and Umlaut.
hi i need to store the weird character in my database but when i am trying to do so the weird character is replace with unknown character. i am giving you one example when i am try to enter in Sönke Wortmann in database it is stored as Sönke Wortmann. i want to store above text as same as it was.please tell me how can i do so
You should use Unicode tables as well as a Unicode connection to your database.
Assuming you use MySQL:
Set the default character set of your table to utf8 and make sure the connection to your database is also using this character set:
$conn = mysql_connect($server, $username, $password);
mysql_set_charset("UTF8", $conn);
See also: http://nl3.php.net/manual/en/function.mysql-set-charset.php
Check the character set of your current connection with:
echo mysql_client_encoding($conn);
See also: http://nl3.php.net/manual/en/function.mysql-client-encoding.php
When creating your tables do something like this:
create table tableName (
// Your table definition
) default charset = UTF8
If you have done these things and add weird characters to your table, you will see it is displayed correct.