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
Related
I tried my best. Still I can't store emojis in the database.
Here is a code which i implemeted.
But the database doesn't support emojis
Please anyone can give me a solution for this?
In databse data stored as ??????
column alter
ALTER TABLE custom_comments CHANGE "c1" "c1" TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL ;
database alter to utf8mb4_unicode_ci
table alter
ALTER TABLE `custom_comments` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
header file header('Content-type: text/html; charset=utf-8');
sqlquery
mysqli_query ($con,"set character_set_client='utf8'");
mysqli_query ($con,"set character_set_results='utf8'");
mysqli_query ($con,"set collation_connection='utf8_general_ci'");
$c1=$_REQUEST['c1'];
$sql_update1 = "INSERT INTO custom_comments (c1) VALUES('".$c1."')";
mysqli_query($con,$sql_update1)or die(mysqli_error($con));
data stored in database as
?ghhhgggggh
??☺
connection
$con = mysqli_connect("localhost","insta_bot","insta_bot","insta_bot");
$con->set_charset('utf8mb4');
In your connection code you are correctly using
$con->set_charset('utf8mb4');
but then in your sqlquery section you change it to utf8 when it should be utf8mb4:
mysqli_query ($con,"set character_set_client='utf8mb4'");
mysqli_query ($con,"set character_set_results='utf8mb4'");
mysqli_query ($con,"set collation_connection='utf8mb4_unicode_ci'");
The MySQL docs describe how these system variables affect the handling of characters.
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 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.
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 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'");