I'm trying to store the following line in mysql:
$data = "After 30μs, what fraction of the charge will remain?"
If I display $data on HTML Page I get the exact text but if i store this in database then i get this data on output I get the following :
After 30Î?s, what fraction of the charge will remain?
I have set charset=utf-8 but even then i'm not able to access the proper data with all the symbols. Is there anything I'm missing ????
You need to set database connection encoding:
mysql_set_charset('utf8');
Or similar method, depending on your database library.
Whats your MySQL server's default charset and collation? Try executing the following query before any other queries inside your scripts.
SET NAMES utf8 COLLATE utf8_general_ci;
BTW, are we talking about a MySQL server?
Make sure your column's charset is utf-8, then use this method for inserting:
INSERT INTO my_table VALUES(
_utf8'After 30μs, what fraction of the charge will remain?', ...
);
and this for reading:
SELECT BINARY my_column FROM my_table;
Related
I'm currently sending info by webservice from Android to a function in PHP which sends that data to the database.
Although, if I have accents in my words like names, I receive this error from PHP:
Incorrect string value: '\xE1\xE1mos ...' for column 'firstname' at
row 1
The names could have accents, like:
António, João, etc.
In PHP function, before inserting into database, I did this without success:
$db->set_charset('utf8');
$query = $db->prepare("SET NAMES 'utf8';");
$query->execute();
If I send the data without any kind of accents, the service works perfectly.
Edit: Solved using utf8_encode(variable).
When sending data to your WebServices in Android, try URL encoding the strings before putting them in the URL/form data, like:
// ...
String encodedName = URLEncoder.encode(name, Http.UTF_8);
// use encodedName instead of name to pass as parameters to the webservices
In the PHP side, before using the parameter, use the urldecode function.
$name = urldecode ($_GET['my_param']);
And keep using the encoding in the database. Also, check if your tables have been created using UTF_8 colation.
It is not enough to specify the encoding of the connection. You need to change the charset of your tables and database to utf-8.
As such, do the following
ALTER DATABASE databasename CHARACTER SET utf8 COLLATE utf8_general_ci;
ALTER TABLE tbl_name CONVERT TO CHARACTER SET "utf-8" COLLATE utf8_general_ci;
I have form with input text, when i add text
Un sac à main de femme recèlerait une quantité importante de bactéries
it adds in database only Un sac
i have tried with addslashes, mysql_real_escape_string, htmlspecialchars etc. also using UTF-8 encoding, but still it can not insert whole string
YOu should use utf8_unicode_ci as your column's collation in orer for French strings to be added in it.
In order to store non-US strings in the database, you must ensure that each of the following 3 steps are correctly implemented:
You database table must be set to a charset compatible with French. To be future proof, I recommend creating tables with UTF-8. For more information see the MySQL documentation.
Your database connection must be set to a proper character set both when storing and when querying. To do this, use mysqli_set_charset() (or whatever your MySQL connector offers).
Your input form AND your view page must be served with the exact character set as your data. To do that, you will need to set the following header: header('Content-Type: text/html; charset=UTF-8'); (If you are using a different charset, change it accordingly.)
You can of course use a different character set for storage and representation but why would you want to do that?
Also, when working with databases and HTML, you should consider:
ALWAYS escape your data as it goes into the database. Use mysqli_real_escape_string() or whatever escape method your database connector offers. Also, do NOT set the connection charset by using SET NAMES UTF8, otherwise your connector library will not know what charset to use for escaping. For more information google "sql injection".
ALWAYS escape your data as it goes into HTML with htmlspecialchars(). Also pay attention to ALWAYS provide the correct character set. For more information google "xss".
After breaking my head for 2 days straight and reading all the possible answers here's what solved the problem and allows me to insert additional weird characters like em dash etc. and retrieve data without seeing weird characters.
Here's the complete step-by-step setup.
The collation of the db column need to be: utf8_general_ci
The type is: varchar(250)
In the PHP header set the default client character set to UTF8
mysql_set_charset("UTF8", $link);
Set the character set result so we can show french characters
$sql = "SET character_set_results=utf8";
$result = mysql_query($sql);
In the html header specify, so you can view the french characters:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
When inserting the data do NOT use utf8_decode, just the below will work fine
$query = 'insert into tbl (col) VALUES ("'.mysql_real_escape_string($variable).'");
Use normal queries to retreive data, example query:
$query = "select * from table;";
Finally got this fixed, hope this is helpful to others.
In the php:
header ('Content-type: text/html; charset=utf-8');
After connection:
mysql_set_charset("utf8");
Just to follow up with this, I was using dbForge Studio and just pasting in French text and I had all the collations/encoding set properly. The one thing I didn't have set was the actual encoding for the connection to the db. Set it to UTF8 and all was well again. #2 in #Janoszen answer.
Had the same problem. The input text came from ANSII file, so it wasn't quite UTF8, despite all my utf8 settings. utf8_encode(input_text) solved it.
I have tried
htmlentities()
. .it saves the string as it is in the database
You should try this to insert special character in mysql :
$con = mysql_connect($server,$uname,$pass);
$res = mysql_select_db($database,$con)
mysql_set_charset("letin1", $con);
From my server, I create and execute a query to my database. The query looks like this:
INSERT INTO `table`(`attr`)
VALUES (
'myText',
...
);
Insert works fine, but when the myText is (for example) the € character, it inserts € (ISO representation). When I execute a query inside phpMyAdmin with this character, it works. Could somebody tell me where is the problem?
NOTE: in PHP, myText, which is inserted into a query, is € - so it´s not converted to ISO representation before the query.
UPDATE: I use MySQL 5.5
UPDATE 2: I have CHARSET=utf8 COLLATE=utf8_bin on my table.
Immediately after mysql connection, before any query, use this:
mysql_connect(...);
mysql_select_db(...);
mysql_query("SET NAMES 'utf8'");
from then on, you can use queries and utf8 chars will be displayed properly.
UPDATE:
Don't forget to put <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> in your document.
Maybe you need to encode the string..
$myText = utf8_encode('MyText');
INSERT INTO `table`(`attr`)
VALUES (
'$myText',
...
);
checkout the charset and collations of your table charset documentation
Your MySql server is not expecting UTF8 by default, run the following query first
SET NAMES 'utf8';
PhpMyAdmin probably has charset configured properly for connection, but your script doesn't.
Make sure you have configured charset correctly for example by statements:
SET CHARACTER SET 'UTF-8';
SET NAMES 'UTF-8';
Before pushing anything to DB (if you're using latin1 instead of UTF-8 as your table charset, you should use latin1).
im facing a really stressing problem here.. i have everything in UTF-8 , all my DB and tables are utf8_general_ci but when trying to insert or update from a single PHP script all i see are symbols.. but if i edit in phpmyadmin the words are shown correctly.. i found that if i run the utf8_decode() function to my strings in php i can make it work, but im not planning to do that because is a mess and it should work without doing that :S
Here is a basic code im using to test this:
<?php
$conn=mysql_connect("localhost","root","root")
or die("Error");
mysql_select_db("mydb",$conn) or
die("Error");
mysql_query("UPDATE `mydb`.`Clients` SET `name` = '".utf8_decode("Araña")."' WHERE `Clients`.`id` =25;",
$conn) or die(mysql_error());
mysql_close($conn);
echo "Success.";
?>
This is what i get if i dont decode utf8 with php utf8_decode function:
instead of Araña, i get : Araña
I've run into the same issue many times. Sometimes it's because the type of database link I'm selecting from isn't the same type that I'm using for inserting and other times, it's from file data into a database.
For the later instance, mysql_set_charset('utf8',$link); is the magic answer.
Place the call to mysql_set_charset just after you select your database via mysql_select_db.
#ref http://php.net/manual/en/function.mysql-set-charset.php
"Araña" IS UTF-8. The characters "ñ" represent the two bytes into which the Spanish ñ are encoded in UTF-8. Whatever you're reading it back with is not handling the UTF-8 and is displaying it as (it appears) ISO-8859-1.
That DDL you mentioned has to do with the collation, not the character set. The correct statement would be:
ALTER TABLE Clients CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
You still need to make sure the client library (libmysql or whatever driver PHP is using) is not transcoding the data back to ISO-8859. mysql_set_charset('utf8') will explicitly set the client encoding to UTF-8. Alternatively, you can send a SET NAMES UTF8; right after you connect to the database. To do that implicitly, you can change the my.cnf [client] block to have utf-8 as the client character encoding (and /etc/init.d/mysql reload to apply). Either way, make sure the client doesn't mangle the results it's pulling.
[client]
default-character-set=utf8
You do not need to use utf8_decode if you're using mbstrings. The following php.ini configuration should ensure UTF-8 support on the PHP side:
mbstring.internal_encoding = utf-8
mbstring.http_output = utf-8
mbstring.func_overload = 6
Finally, when you display the results in HTML, verify that the page's encoding is explicitly UTF-8.
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.