can not synch PHP and sql charsets - php

I have a confusing problem with php and css.
I created a database in phpmyadmin. All fields collation is utf8_general_ci. Also my table and database use the same collation as the fields. I tried every way to store data in database like using VALUES (N'content').
the most confusing problem is when I insert data using php my admin it shows at the webpage with question marks. When I store with PHP with the same SQL code it shows fine at the webpage but in the database strings shows like this:
تست Ùارسی
I really tried everything, can anyone help me through this problem?

utf8_general_ci isn't a character set, it's a collation. The collation determines how MySQL will decide how to order things when you ask it to sort alphabetically, it doesn't set the actual charset. Look at the table and the database character set, it should be UTF8 or UTF8MB4. If it's not then you've got a character set mismatch.

Related

Strings stored in a wrong charset on database

I have a problem with varchar elements stored in a database.
In order to be printed in the correct way they have been stored in a strange collapse that i can not understand, see this example:
Data Stored incorrectly
You can notice that €™ in the strings. It should be an "è". Same things happen for ' and for "à". How can i change the character set of the database in order to display properly the varchar elements?
Currently settings are:
Storage engine: MyISAM
Collation: latin1_swedish_ci
Thank you very much. I really need help. I tried a lot of collations but nothing seems to work.
As far as I know, collation is for ordering and comparison, not recording.
You might be looking for encoding instead as it is responsible for the characters itself.
I believe that your issue is that you have the wrong encoding. Before you you set it to UTF8, please change your collation to utf8_unicode_ci.
Please attempt to run this:
SET NAMES utf8;
That should resolve your issue for new records. The old ones will remain untouched.

PHP 5.6 encoding latin1 MySQL encoding

After migration from PHP 5.3 to PHP 5.6 I have encoding problem. My MySQL database is latin1 and my PHP files are in windows-1251. Now everything is displayed like "ñëåäíèòå àäðåñè" or "�����".
It should be display something in Cyrillic like "кирилица". I've tried mysqli_set_charset but it didn't solve my problem.
First, let's see what you have in the table. Do SELECT col, HEX(col)... to see how these are encoded. Here is the HEX that should be there if it is correctly utf8-encoded:
ñëå --> C3B1C3ABC3A5; кир --> D0BAD0B8D180
If you don't get those, then the problem was on inserting, and we may (or may not) be able to repair the data. If you have C390C2BAC390C2B8C391E282AC for the Cyrillic, then you have "double encoding", and it will take some work to 'fix'.
utf8 needs to be established in about 4 places.
The column(s) in the database -- Use SHOW CREATE TABLE to verify that they are explicitly set to utf8, or defaulted from the table definition. (It is not enough to change the database default.)
The connection between the client and the server. See SET NAMES utf8.
The bytes you have. (This is probably the case.)
If you are displaying the text in a web page, check the <meta> tag.
Halfer is right. Change both your PHP and MySQL encoding, first the PHP with
mb_internal_encoding ("UTF-8");
mb_http_output("UTF-8");
to UTF-8, at the top of your PHP pages.
If you miss out the "UTF-8" and print the output from these finctions, it will show you your current PHP encoding - probably windows-1251
Also note that with MySQL you need to change the character encoding on the row in the table as well as on the table itself overall and on the database itself overall, as the defaults will remain latin1 so any new fields you add would be latin1 without being carefully checked.
If you are trying to save Cryllic text to the database you will need the correct Cryllic character set in the database, rather than latin1

mysql shows chinese characters like squares

I want to save chinese characters in mysql db, charset is set to UTF8 via connecting to db, also the field's charset is utf8, and collation - utf8_general_ci,
But instead of the word it shows squares. I use sqlyog.
There is one thing, if I make request and echo the word in the browser if shows the right chinese word.
So, I am wondering why it shows the correct word in browser, when in db it is like squares and vice versa.
I am afraid that maybe via exporting or importing in the future I can have some data lose.
Thanks
Your data might be stored correctly in the DB, but read wrongly by sqlyog.
I haven't used sqlyog, but this problem might be because of the way sqlyog connects to MySQL - look for parameters in sqlyog connection to DB that are related to character set and make sure they are also utf8
I had a similar problem when I had to insert Latin characters to the database, I used mb_convert_encoding($str, 'utf8', 'HTML-ENTITIES') and it got stored correctly in the database and wen I had to show it in the html page I just had the encoding=utf-8

Mysql Collation for arabic using php

I am facing problem with mysql database. I cant save arabic text into the mysql data even i change the collation to cp1256_general_ci and tried other collation. I cant get much help from search.
Anyone who can help me out please help
I have change collation at database level as well as colum level to cp1256_general_ci for some fields.
Please suggestion how should i set this as i am NEW PHP and MySQL
I also write simple INSERT statement to insert input data in mysql do i have to take any case while inserting data into mysql if it is in arabic
The short answer is: Use UTF-8 everywhere. "Everywhere" means
In all forms that are used to store data in your database
The database connection
The database tables and columns
In all pages that output data from the database.
if you have existing CP-1256 data (or incoming data in that character set that you can't change) you can use iconv() to convert it into UTF-8.
Note that if using UTF-8, you need to make sure you use multibyte-safe string functions. This is often already the case because standard function like strlen() get mapped to mb_strlen(). To find out whether this is the case on your server, see the manual entry on the issue.

How to save other languages in mysql table?

Hi I have to save hindi languages in mysql. how can I do that. any one knows the solution please help me.
You need to store all text as UTF8, then you'll be able to see Hindi characters. You can update a column to use UTF8 with a query like the following:
ALTER TABLE posts MODIFY title VARCHAR(255) CHARACTER SET UTF8;
Since you use PHP, make sure that all your PHP scripts are saved as UTF8. You can also set the connection charset with the following query:
SET NAMES 'utf8'
This will ensure that your web server and database servers communicate using UTF8.

Categories