I have done a code in PHP to insert data into MySql in malayalam. When I insert data, It becomes English in database.. I have set charset to utf-8 in my code and in data base utf8 general_ci..please anyone give answer..thanks in advance
You can give this simple code
SELECT * FROM dictionary WHERE malayalam = 'ആകാശം'
This code will work fine in phpmysql,also you can refer this link
Press this link for your clarification
1) Add the code beginning of the php page
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
2) Make sure your database & tables are on Collation utf8_general_ci
3) Use the database connection code:
$conn = new mysqli($server, $user, $password, $database);
$conn->set_charset("utf8");
4) Then try insert query and retrieve query using $conn handler,
ie: $q_newspage="SELECT * FROM news GROUP BY id DESC LIMIT 8";
$qresult_newspage=$conn->query("$q_newspage");
Thanks
Don't forget to set charset as utf8 in META tag
<meta charset="utf-8">
For More
And in mysql as
mysql_set_charset('utf8');
Then you can insert in to db as,
INSERT INTO tableName (name) VALUES ('രാകേഷ്');
Related
I've read many threads on Stack and other forums, but still can't figure how to make it work.
The configuration
I have a MySQL 8.0 database named test, and configured it with default CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_as_ci.
Inside there's a table creatively named table, this too configured as utf8mb4 and utf8mb4_0900_as_ci.
The only field is named field and is a blob type, where I write and read encoded data using AES_ENCRYPT/DECRYPT
VBScript classic connect to it using the last ODBC 8 UNICODE driver
I've tried the SET NAMES statement but is not supported in this ODBC version. Instead the charset is included directly in the connection string.
Following other threads, I made a test file
without any external include
saved as UTF8
specified all the possible Vbscript Codepage and Charset at the beginning of the file
the connection string specifies a charset, I've tried both ucs2 and utf8mb4
the html segment specifies the utf charset both in the Content Type and in the form
when reading from the database I specify to CONVERT USING utf8mb4
The page is setup to be the simplest test possible, with a form on top, and the results listed below.
All the simple text is processed correctly, while any emoji is read as ?
For example, if this is the text passed through the form -> hello 😀 😃 😄 😁 😆 😅 😂
this is what is returned -> hello ? ? ? ? ? ? ?
Executing the same SELECT query in Workbench 8, shows the correct text with the emoji, so it's not a problem directly related to the Codepage or Charset, or when it writes the data, but only when it read them.
VarType report the RS("Field") as a simple string / 8.
I've spent the last days studying and testing all the possible solutions, but can't solve it :/
<%#Language="VBScript" CodePage="65001"%>
<% Option Explicit %>
<%
' THE PAGE ENCODING
Response.ContentType = "text/html;charset=UTF-8"
Session.CodePage = 65001
Response.CodePage = 65001
Response.CharSet = "UTF-8"
'----------------------------------------------------------------
dim dbConn, sql, RS
' SIMPLE CONNECTION STRING
' NOTE THAT I'VE TRIED BOTH charset=ucs2 AND charset=utf8mb4
Set dbConn = Server.CreateObject ("ADODB.Connection")
dbConn.Open "DRIVER={MySQL ODBC 8.0 UNICODE Driver}; SERVER=127.0.0.1; PORT=3306; DATABASE=test; Uid=user; Pwd=password; charset=ucs2;"
'----------------------------------------------------------------
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>test MySQL UTF-8</title>
</head>
<body>
<form action="?action=insert" method="post" accept-charset="utf-8">
<textarea name="text"></textarea>
<input type="submit" value="Insert">
</form>
<%
'----------------------------------------------------------------
' INSERT THE STRING WHEN THE FORM IS SUBMITTED
If Request.Querystring("action") = "insert" Then
sql = "INSERT INTO table (Field) VALUES(AES_ENCRYPT('" & Request.Form("text") & "', 'AES_Key'))"
Response.Write(sql & "</br>")
dbConn.execute(sql)
End If
'----------------------------------------------------------------
' LIST ALL THE RECORDS FROM THE TABLE
sql = "SELECT CONVERT(AES_DECRYPT(Field, 'AES_Key') USING utf8mb4) AS Field FROM table"
Set RS = dbconn.execute(sql)
Do Until RS.EOF
Response.Write(RS("Field") & "</br>")
RS.MoveNext
Loop
RS.close
dbConn.close
%>
</body>
</html>
I've create a form that contains Hindi (UTF-8) data which i want to store in MySQL table. The columns corresponding to UTF data has collation value set to utf_general_ci.
I've successfully stored the data in table but when I'm executing a select-where query, it doesn't returns the data. Here is my query:
SELECT Birth.sno, Birth.bookingnumber, Birth.birth_date, Birth.baby_gender, Birth.baby_name, Birth.baby_father_name, Birth.baby_father_address, Birth.baby_mother_name, Birth.birth_place, Birth.place_type, Birth.applicant_name, Birth.applicant_address, Birth.registration_number, Birth.registration_date, Birth.registration_ward, Birth.registration_city_village, Birth.registration_district, Birth.remark, Birth.mother_place_name, Birth.mother_place_type, Birth.mother_place_district, Birth.mother_place_state, Birth.person_religion, Birth.father_education, Birth.mother_education, Birth.father_occupation, Birth.mother_occupation, Birth.mother_age_at_marriage, Birth.mother_age_at_birth, Birth.count_of_mother_child, Birth.birth_by, Birth.birth_method, Birth.mother_weight_at_birth, Birth.pregnancy_duration, Birth.date_of_issue FROM np.births AS Birth WHERE Birth.baby_name = 'd' AND Birth.baby_father_name = 'e' AND Birth.baby_mother_name = 'f' AND Birth.baby_father_address = 'g' AND Birth.person_religion = 'हिंदू' AND Birth.baby_gender = 'पुरुष'
The name of the database is np and name of the table is births
The above query was printed in the log file. I tried to copy and paste the same query in HeidiSQL (front end for MySQL) but its not running. However, if I remove the following part: ** AND Birth.person_religion = 'हिंदू' AND Birth.baby_gender = 'पुरुष'**, the query works fine.
How can I resolve this issue?
This looks like a case when your MySQL client and your MySQL server do not "talk" the same encoding.
There are 3 places where you need to take care of your encoding.
The Web Form (what the users sees) -> Your Web Application (CakePHP) -> Your Database Server (MySQL)
One of those three is NOT using the same encoding as the others. So by the time:
"'हिंदू'" and "'पुरुष'" get to your database they will be something totally different that will not be found in the database.
So, make sure that in your default.ctp file you have set your encoding:
echo $this->Html->charset(); //this will result in a UTF-8 encoding of the page.
Look at the source code of your web page (where I guess you have a search/filter form).
At the top you should see:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Then look for the code generated for your search/filter form. You should see:
<form id="your_form_id" accept-charset="utf-8" method="post" action="/your/action/">
The important part is that it that "utf-8" that MUST show up in those places.
Next, look into your database.php file and make sure this line:
'encoding' => 'utf8', is NOT commented out!
Finally, with a client that you are sure supports UTF-8 (probably HeidiSQL) have a look at your data table np.births and make sure that what data you have there actually makes sense! It's possible it got mangled because of the discrepancies in encoding before.
Once the data makes sense in the database you should be good to go!
IF this does not do it you, you'll have to read and thoroughly understand this article. Only then you will be able to locate where the problem is and get your encodings in sync.
(Obviously your PHP source files should be UTF-8 encoded as well...)
I'm getting encoding problem when I insert names in a mySQL table, that contains special characters, like "ö", "ä" etc.
For example, the word "Öl" becomes "öl".
I've tried to write the names to a text file and then they show up properly.
I've tried to insert the names in phpMyAdmin using SQL-statements, and that works good as well.
Now I found a solution in setting mysql_query('SET NAMES utf8;'); before the insert query.
Is this how it should be done, or is there a better way?
yeah running the SET NAMES utf8; is needed to make the MySQL know that the client connection is using ut8 while sending the data. You can though now define it inside the PDO connection (if you are using PDO for connecting to the MySQL).
If running PHP version older than 5.3.6 then you can use the following code:
$pdo = new PDO(
'mysql:host=mysql.example.com;dbname=example_db',
"username",
"password",
array(PDO::MYSQL\_ATTR\_INIT\_COMMAND => "SET NAMES utf8"));
else use the following:
$pdo = new PDO("mysql:host=localhost;dbname=world;charset=utf8", 'my_user', 'my_pass');
I am trying to save Arabic language in mysql database but it doesnot save in Arabic format.
It shows question marks instead of Arabic. How to make it store values in Arabic.
I tried many queries seeing from internet but it doesnot changes. How to change it for Arabic.
"ar_SA: Arabic - Saudi Arabia"
Please suggest a way?
i use WAMP Server. (windows,apache,mysql,php).
//so important
FIRST :
in phpmyadmin or MySQL :
make sure that Mysql Database is utf.
make sure that the your database and it's tables are utf-general-ci
after connecting to Mysql immidiately (before choosing your DB) make this order.
mysql_set_charset('utf8');
example :
<?php
//connect to MySQL
mysql_connect("localhost", "user", "password") or die(mysql_error());
mysql_set_charset('utf8'); // that's the order.
echo "Connected to MySQL<br />";
//connect to your DB
mysql_select_db("mydb") or die(mysql_error());
echo "Connected to Database";
?>
SECOND :
in the meta data in the php file make the meta data as following :
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Third :
Make sure that the php file it self is utf-8 enabled . u can make sure in your IDE settings , or if u work on notepad++ or Komodo Edit then u can find it in the status bar at the bottom of the window , right side.
// i tried this but it didn't have effect.
in the header of the php file (before every thing).
<?php header("Content-type: text/html; charset=utf-8"); ?>
in the form submitted:
<form accept-charset="utf-8" ...>
Just use UTF-8 in the page's encoding, in the database connection, and the database itself.
Make sure your database encoding and collation is utf8_general_ci
Can someone explain me when I set everything to UTF-8 I keep getting those damn ���
MySQL
Server version: 5.1.44
MySQL charset: UTF-8 Unicode (utf8)
I create a new database
name: utf8test
collation: utf8_general_ci
MySQL connection collation: utf8_general_ci
My SQL looks like this:
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
CREATE TABLE IF NOT EXISTS `test_table` (
`test_id` int(11) NOT NULL,
`test_text` text NOT NULL,
PRIMARY KEY (`test_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `test_table` (`test_id`, `test_text`) VALUES
(1, 'hééélo'),
(2, 'wööörld');
My PHP / HTML:
<?php
$db_conn = mysql_connect("localhost", "root", "") or die("Can't connect to db");
mysql_select_db("utf8test", $db_conn) or die("Can't select db");
// $result = mysql_query("set names 'utf8'"); // this works... why??
$query = "SELECT * FROM test_table";
$result = mysql_query($query);
$output = "";
while($row = mysql_fetch_assoc($result)) {
$output .= "id: " . $row['test_id'] . " - text: " . $row['test_text'] . "<br />";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="it" xmlns="http://www.w3.org/1999/xhtml" xml:lang="it">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>UTF-8 test</title>
</head>
<body>
<?php echo $output; ?>
</body>
</html>
Try to set charachter encoding after mysql_connect function like this:
mysql_query ("set character_set_client='utf8'");
mysql_query ("set character_set_results='utf8'");
mysql_query ("set collation_connection='utf8_general_ci'");
I set everything to UTF-8
Not quite.
You have to tell mysql your client's encoding.
As a matter of fact, you don't have to set up "everything" in utf-8. You can have your tables in latin1 and output in utf-8. Or contrary.
Very flexible.
But you have to set up client's encoding explicitly.
So, that's why it works with set names utf8. Because this query setting up client's encoding. And let Mysql know that data must be sent in utf-8. Pretty sensible, huh?
Also I have to mention your SQL dump. It needs same setting. Just SET NAMES somewhere at the top. Because you are sending these queries from some client too. And this client's encoding needs to be set up as well.
And one more thing to mention: be sure your server sending proper encoding in the Content-type header. You didn't set it to UTF-8 too.
I didn't see a "SET NAMES 'utf8';" query just after connecting to your database.
Try it, may work for you.
I would say that you forget to set the content type encoding of your PHP file to utf-8:
header('Content-Type: text/html; charset=utf-8');
Or is the encoding error within the MySQL database?
If only loading the data returns the wrong results, you can use the queries mentioned before or this line of code to enable UTF-8 for queries:
$mysqli->set_charset('utf8');
I hope that is what you needed.
take a look at the mysql_set_charset function. Perhaps you need to call it before you retreive the data.
You want to check the current charset using mysql_client_encoding and when needed mysql_set_charset. Or just never mind the checking and blindly go with setting.