Mysql - Insert queries inserting funny characters - php

I have a simple script which inserts values from a text file into a mysqldatabase - however some accented characters aren't inserted properly. Like lets say I have a word:
Reykjavík
I try to insert it using a simple insert sql statement and instead I this value ends up in the database????
Reykjavík
How do I fix this?
====EDIT====
I tried to change the collation - the thing is that I'm using navicat here and if I lets say try to insert any such word with accentuated characters like this using the navicat QUery generator or panel it is inserted perfectly with no problem whatso ever - however my php script when it runs a similar query end s up putting trash in the table :( whats going on here...

The set of questions marks is because you are inserting a character whose encoding is not understood by MySQL. Try collating with utf8_bin. This should work.

Related

Unable to insert square root string in my mysql database

I want to insert similar text into mysql table.
It is inserted in table but it insert like 42=(-4)2=16 ..
I want to insert it properly with square root symbol.
I guess MySQL proper collation type might be the solution but don't know which collation it may work.
As you described you tried the right collation (utf8_unicode_ci), it is not a MySQL issue. You may see the previous discussion on how to store unicode in mysql on stackoverflow.
Following these answers, I executed the followed queries:
CREATE TABLE can_square ( sqr CHAR(100) CHARACTER SET utf8 COLLATE utf8_unicode_ci);
INSERT INTO can_square (sqr) VALUES ( "4² = (-4)² = 16" );
And it works fine.
And I inserted the same type of data from my PHPMyAdmin successfully, both from running an SQL query and inserting manually in the insert tab. Then it also is not a PHPMyAdmin issue
As yourself spotted, its probably a string formatting issue when you copy and paste from another source. Try to type the string directly in PHPMyAdmin and see if it works. If you have to put the value manually, and you know it is happening, copy and paste may not be a good alternative.

Where does character encoding change?

I have a MySQL database with a table with entries with accents, like "João".
The entries are selected with a MySQL++ query in C/C++ code, but this entry in particular is printed as "Jo�o" (printf, fprintf or std::cout <<).
What I'd like to understand is: where in the data flow is this character being encoded incorrectly?
Some more context: the front-end is HTML/PHP, which uses PDO to insert the data into the MySQL database.
I see the character correctly displayed with PDO queries in HTML. It is also correctly displayed with:
mysql> select * from <table>;
so I assume it is well written in the table. The problem seems to reside either with the MySQL++ query or the C/C++ output command.
I don't know if it is relevant, but MySQL's table encoding is utf8_general_ci and shell locale is LANG=en_US.UTF-8.
I got the answer from this post: http://forums.mysql.com/read.php?167,243667,243695#msg-243695
The problem is with MySQL++. After creating the connection, we need to execute:
mysqlpp::Query query = connection->query("SET NAMES 'utf8'");
query.execute();

PHP Chinese Characters Can't Insert To Mysql

I am using GET to pass parameters, succesful save data to mysql if the data is in english, but nothing insert if the data is in chinese.
I have detected the data encoding is utf-8, found out that using my macbook it works fine, but not working on ie. Strange, any idea what's wrong with it?
For example
query("insert into Info values ('".$email."','".$_GET['name']."')");
This not successful if $name is chinese. I have tried echo $_GET['name'] and display normally, howerver the data can't inserted to databse is blank.
query("insert into Info values ('".$email."','香港')");
This method successful insert data to database
Firstly, you really should use POST for these operations.
Check to ensure that the column you are inserting into has the correct encoding - perhaps export the table structure for us to see if you're not sure.
As Daan said, you could try
query('SET NAMES UTF8');
Before your insert query.

UTF8 database querying and special characters

I have a Mysql database (in utf8_general_ci) and have to query columns containing special characters. The problem is: queries containing special characters do not retrieve any results – but queries containing the regular version of those characters do.
For instance, if I want to query the word “été”:
SELECT … WHERE column LIKE % été % will not work.
SELECT … WHERE column LIKE % ete % will work!
This problem must be due to my encoding (both queries work when done directly in phpMyAdmin).
I use PDO and followed the instruction here (http://stackoverflow.com/questions/279170/utf-8-all-the-way-through) and, when connecting to the database, do exec(“SET CHARACTER SET utf8”) AND exec(“SET collation_connection = ‘utf8_general_ci’”). What bothers me is that, excepted for this search query, the website seems to work perfectly with utf8 both for the display and the database.
I thought that I could operate a transliterate_to_ascii on the strings containing special chars. I’m however afraid that this option is suboptimal, especially for non european languages.
Thank you very much for your help!
Please try running SET NAMES utf8; on mysql right after the connect

PHP MySQL insert dropping data

Ok this is a new one for me. Basically I have a table articles:
id: int auto increment
title: varchar(200)
description: varchar(1000)
ctext: longtext
chtml: longtext
Now I do an insert into this table with mysql_query:
INSERT INTO articles
(title, description, ctext, chtml)
VALUES
('$title', '$description', '$text', '$html')
All values have been passed through mysql_escape_string().
The text and html here are roughly 50k in size (so I can't really post the fully query here).
Now, here's the problem: the query works. A new row is inserted. However the ctext and chtml columns are empty. This is MySQL 5.0.51a and PHP 5.2.8. No errors are raised of any kind as far as I can tell.
Now I dumped the query out to a file in /tmp and ran it with:
mysql -u username -p dbname < /tmp/query
Same thing.
I copy the query into Navicat and it... works.
So what on earth is going on?
Some random thoughts:
Have you tried controlling the text length see if it only fails at one point?
What kind of connection are you opening? Which driver?
Have you checked the encoding of your connection? Some invalid characters might be sent in.
Have you tried using parameters instead of mysql_escape_string?
Have you tried executing directly from the same file from Navicat instead of using the copy-paste? Again, might be related to an invalid character that's not passed through the copy-paste but was saved in the file.
Just to cover the basics we so often forget, how do you verify that the data is not inserted? I mean, how to you visualize it? You could have a line break that hides the first lines from 2 out of 3 means of visualization. Just a long shot, but I've seen it happen.
Addition: MySQL connections defaults to latin1, you need to use something like mysql_query("SET NAMES 'utf8'") to transfer unicode characters.
I'm not sure if this matters, but mysql_escape_string is deprecated and replaced by mysql_real_escape_string
Have you tried it with smaller text?

Categories