Sean Bone
Sean Bone

Reputation: 3546

Problem with charset when inserting text into a MySQL DataBase with PHP

I'm trying to insert text into a MySQL DataBase from a form using PHP.
Problem is, when I check the DataBase in phpmyadmin, I find that special characters like ü, ö, ä, è, é, à... are being corrupted, and I get things like üöäéèÃ.I've tried the following:

I noticed that running the query directly in the phpmyadmin the values weren't corrupted, so I suppose the error must be somewere in the PHP and/or the MySQL connection.
Also, I tried doing the same with utf-8_general_ci, and obtained same results. It seems to be ignoring anything I do.
Advice and ideas welcome. Thanks guys,
Sean

Upvotes: 2

Views: 10076

Answers (6)

Jadeye
Jadeye

Reputation: 4279

For those Hebrew users:

HTML PAGE:

<html lang="he">
  <head>
    <meta charset="utf-8">

PHP FORM

header('Content-Type: text/html; charset=UTF-8');
$con = mysqli_connect("HOST","DB_USER","PASSWORD","DB_NAME");
$con->set_charset('utf8');

DB, TABLE, FEILDs COLLATION

utf8_general_ci

Upvotes: 0

SagarPPanchal
SagarPPanchal

Reputation: 10111

You may user

mysql_query("set character_set_results='utf8'");

before executing MySQL query

Upvotes: -1

hakre
hakre

Reputation: 197599

I can not properly say if that is the cause of your problem, but in any case you can not put a Mysql specifier of charset and collation neither into the HTML form nor into PHP.ini.

Right now you use the MySQL value somewhat blindly and put it everywhere you assume it fit - without even knowing. Don't do that. First of all decide which encoding you want to use and accept. From your question I would say that is latin1.

So configure HTML and PHP appropriately:

<form accept-charset="latin1">

For the HTML form element. Note that there is no latin1_swedish_ci in HTML, see the HTML docs.

A next and similar point is in your php.ini setting. There doesn't exists something like you put there. Change it to latin1 as well:

default_charset = "latin1"

With these two settings corrected, the browser is aware what the encoding of the page is that contains the form, and the form even signals the browser the accepted encoding of the data passed back to the server.

So this should ensure that the data you have inside the PHP variables is latin1 encoded. You can then pass this data into your database if you properly configured the encoding of the database client, the database server, the encoding of the connection between the two and naturally the encoding of the data in the database as well.

However, the proper setup of the form is the very first step you need to do before you can ensure that the mysql connection isn't wrangling stuff up.

Upvotes: 2

Jernej Jerin
Jernej Jerin

Reputation: 3389

I think you should set character enconding of the page to:

<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />

Upvotes: 0

YonoRan
YonoRan

Reputation: 1728

Not sure if you tried this or not... but I had kind of the same problem while inserting Hebrew into a MySQL database. I did the following: Database tables are collation is: utf8_unicode_ci

In PHP I do this:

@ $db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);
$db->set_charset('utf8');

I create a new connection and than I specify the connetion as UTF-8.

This solved my problem when dealing with Hebrew...

Upvotes: 4

Mohamed Said
Mohamed Said

Reputation: 4613

Make sure the collation and the MYSQL connection collation are both latin1_swedish_ci

Upvotes: 0

Related Questions