adding hebrew value to sql using PDO - php

I am trying to add a Hebrew value to a database using php's PDO class.
After finally succeeding doing that
(by using: array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"), I found out that COLLATE HEBREW_CI_AS is added to my value.
Is there any way to prevent this addition?
thank you, and sorry for my bad English.

PHP: The Right Way is a good source of information on this.
Basically check whether your PDO-connection specifies the charset in the DSN:
new PDO(
'mysql:host=your-hostname;dbname=your-db;charset=utf8',
'your-username',
'your-password',
array(
# ... PDO Connection Options
)
);
Make sure your database tables are set to use the utf8-collation, either by phpmyadmin or using the mysql-command line utility and the following query:
ALTER TABLE table_name COLLATE utf8;
Also you might want to consider using utf8mb4 as is suggested in the book.

Related

PDO, mysql, utf8 (arabic text) doesnt show in php

I have the following php code. Connection is OK but it doesn't show the Arabic text stored in the database correctly. Just question marks.
$mysqlPDO = new PDO('mysql:host='.HOSTNAME.';charset=utf8;dbname='.DBNAME.'',DBUSERNAME, DBPASSWORD, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES UTF8"));
$stmt = $mysqlPDO->prepare("SHOW TABLES LIKE 'main_patches_version'");
$stmt->execute();
The DB collation is already set to utf8_general_ci and the table has utf8 as charset.
This application is calling Zend libraries which I'm not aware of.
I checked that the HTML has utf8 as encoding type. any suggestion?
I'm using xampp server, php version 5.5.11, mysql version 5.6.16
I found the solution here
http://akrabat.com/php/utf8-php-and-mysql/
I unmarked
character_set_server=utf8 from my.ini file and it works.
Thanks all
Make sure your DB collation is set to use utf8_general_ci or utf8mb4_general_ci.

insert Arabic values to mysql

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

How to encode cyrillic in mysql?

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.

Zend DB and encoding

I have just encountered something rather strange, I use the Zend Framework 1.10 with the Zend_Db_Table module to read some data from a databse. The database itself, the table and the fields in question all have their collation set to "utf8_general_ci" and all special chars appear correctly formatted in the DB when checked with phpMyAdmin. Also, saving with Zend_Db_Table works just fine, yet when I read the data and just echo it to my browser it is returned as ISO-8859-1, not as UTF8. I noticed the same thing when trying to use json_encode (which only works with UTF8 strings as input) on a value returned from the DB.
How can I set that Zend_Db_Table/Zend_Db_Row should always work with UTF8 and return me an UTF8 value? I have not set anything regarding encoding in my app yet.
Thanks a lot for your help!
Just note. In my case this one helped:
$this->db = new Zend_Db_Adapter_Pdo_Mysql(array(
'host' => $config['db_hostname'],
'username' => $config['db_username'],
'password' => $config['db_password'],
'dbname' => $config['db_database'],
'charset' => 'utf8'
));
resources.db.params.charset = utf8
like robertbasic said.
Ok just found the solution, try to do this:
$db = Zend_Db::factory($config->database); // Setting up the DB
$db->query("SET NAMES 'utf8';"); // That's the magic line I was missing
Hope this helps somebody else at some point :)
Also you can just put "charset" key with desired value into config and DB-driver will execute an appropriate query (depending on DBMS used). It seems to be that currently (version 1.10.5) almost all drivers support that.

UTF-8 Database Problem

I've a MySQL table that has a UTF-8 charset and upon attempting to insert to it via a PHP form, the database gives the following error:
PDOStatement::execute():
SQLSTATE[HY000]: General error: 1366
Incorrect string value: '\xE8' for
column ...
The character in question is 'è', yet I don't see why this should be a problem considering the database and table are set to UTF-8.
Edit
I've tried directly from the mysql terminal and have the same problem.
Your database might be set to UTF-8, but the database connection also needs to be set to UTF-8. You should do that with a SET NAMES utf8 statement. You can use the driver_options in PDO to have it execute that as soon as you connect:
$handle = new PDO("mysql:host=localhost;dbname=dbname",
'username', 'password',
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
Have a look at the following two links for more detailed information about making sure your entire site uses UTF-8 appropriately:
UTF-8 all the way through…
UTF8, PHP and MySQL
E8 is greater than the maximum usable character 7F in a one-byte UTF8 character: http://en.wikipedia.org/wiki/UTF-8
It seems your connection is not set to UTF8 but some other 8 bit encoding like ISO Latin. If you set the database to UTF8 you only change the character set the database uses internally, connections may be on a different default value (latin1 for older MySQL versions) so you should try to send an initial SET CHARACTER SET utf-8 after connecting to the database. If you have access to my.cnf you can also set the correct default value there, but keep in mind that changing the default may break any other sites/apps running on the same host.
Before passing the value to Mysql you can use the following code:
$val = mb_check_encoding($val, 'UTF-8') ? $val : utf8_encode($val);
convert the string the to UTF-8, If it's matter of only one field.

Categories