Link to home
Start Free TrialLog in
Avatar of Marco Gasi
Marco GasiFlag for Spain

asked on

Codeigniter, MySql and utf-8

Hi everybody.
I'm working on an application which builts a form from the mysql table and then post back values to the table itself. Everything works fine, but...
Now I have added to the table some column and I have used spanish names. Among them I have used 'baños'.
Now the form is correctly displayed, the label in the form is correct and even the database error displays correctly the world 'baños' among the columns of the table... But the query fails because suddenly in the $_POST array appears a  value without name (indexed to 0) and set to the same value of the input 'baños'.
In other words, the input baños is inserted in the $_POST array 2 times: associatively and numerically!:
This is the error mesages I get:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '0) VALUES ('GUIA DE ISORA CASA INDEPENDIENTE DE 3 HABITACIONES DE 192 m2 (COD. 6' at line 1

REPLACE INTO `immobiliaria` (`title`, `dormitorios`, `baños`, `garaje`, `piscina`, `hamaca`, `zona`, `precio`, `codigo`, `medidas`, `notas`, 0) VALUES ('GUIA DE ISORA CASA INDEPENDIENTE DE 3 HABITACIONES DE 192 m2 (COD. 625)', '', '2', '', '', '', '', '', '', '', '', '2')

Open in new window


The database colation is utf8_general_ci, Codeigniter is set to utf-8 charset too...

Have someone any idea? Keep in mind that if I just rename the column 'baños' to 'banos' everything works fine...

Thank you for your help, guys.
SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Marco Gasi

ASKER

Hi Ray. Yes, I have checked character set infos twice before to post :) My table is set to utf-8, CodeIgniter does too (how can I check php character set, since I can't use phpinfo() due to my shared server security limitations?)

Anyway, I had already managed using the workaround you have suggested: using 'banos' instead of 'baños' fixes the issue perfectly, but it looks bad to me having to setup such a workaround. What's puzzling me is the type of issue: the same input replicated with key 0 instead of its name. I would expect the name itself be displayed erronously but it isn't.

If I use var_dump() I get (for testing purposes I just filled one input):
array(12) {
  ["title"]=>
  string(0) ""
  ["dormitorios"]=>
  string(0) ""
  ["baños"]=>
  string(1) "2"
  ["garaje"]=>
  string(0) ""
  ["piscina"]=>
  string(0) ""
  ["hamaca"]=>
  string(0) ""
  ["zona"]=>
  string(0) ""
  ["precio"]=>
  string(0) ""
  ["codigo"]=>
  string(0) ""
  ["medidas"]=>
  string(0) ""
  ["notas"]=>
  string(0) ""
  [0]=>
  string(1) "2"
}

Open in new window

The last array element is the numerically indexed representation of the element 'baños': how a character set collision can bring to this? It's crazy!
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That looks like a suitable workaround.  Probably best to leave a large comment in the code explaining why you had to do that, otherwise someone will look at one day, puzzled, and say "this shouldn't be here."

Glad you've got it pointed in the right direction!
I have found an workaround wich, though inelegant, fixes this obscure issue :)
Thank you