0

I wrote a PHP file which should insert values in a MySQL database.

I explain my problem with an example.

include 'db_connect.php';
mysql_query("SET NAMES 'utf8'");
mysql_query("SET CHARACTER SET 'utf8'");
$title = 'knödel';
echo $title;
$sql="INSERT INTO RECIPES (title) VALUES ('$title')"; 

In the database, there is 'kn';

I cut the string from the umlaut.

7
  • 1
    What is the character encoding of the table? Both the connection charset (which you've set) and the table charset should be set to the same value. Commented Jan 20, 2012 at 8:00
  • Is the column defined to be utf8? Commented Jan 20, 2012 at 8:02
  • your database column should be UTF-8 Commented Jan 20, 2012 at 8:04
  • it ist utf8_bin but it won´t work. Commented Jan 20, 2012 at 8:14
  • 1
    OK. Go back and accept the useful answers to your previous questions. You might want to add your solution as an answer to your question and accept it too. Commented Jan 20, 2012 at 9:11

1 Answer 1

1

If your database collation is Unicode or UTF-8, just do:

$sql="INSERT INTO RECIPES (title) VALUES (N'$title')"; 

The only difference is the N character which makes it possible to insert Unicode data into the database.

And you have to take the security risks seriously. Use mysql_real_escape_string together with other required actions to protect against sql injections. (If you don't do it now, you'll forget it when writing a big real-world application.)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.