CodeIgniter Forums
Error inserting special characters (spanish) into database - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Error inserting special characters (spanish) into database (/thread-16622.html)



Error inserting special characters (spanish) into database - El Forum - 03-12-2009

[eluser]jorgeakanieves[/eluser]
Bufff...
I´m trying to insert special characters ( accent '´' and question character '¿' ) but the inserted field is null. If I do the query in phpmyadmin directly, it´s inserted all right.

I don´t know whay may be happening because I´ve defined:

database.php:
Code:
$db['default']['char_set'] = "utf8";
$db['default']['dbcollat'] = "utf8_general_ci";

And with the phpmyadmin client, my database, tables and fields are set to:
database: utf_spanish_ci
table: utf_spanish_ci
field: utf_spanish_ci

In the config file of mysql ( my.ini ):
Code:
[mysql]
default-character-set = utf8_general_ci
[mysqld]
default-character-set = utf8_general_ci
character-set-server = utf8
collation-server = utf8_general_ci
init_connect = 'SET collation_connection = utf8_general_ci'
init_connect = 'SET NAMES utf8_general_ci'

What´s the error?


Error inserting special characters (spanish) into database - El Forum - 03-12-2009

[eluser]xwero[/eluser]
You could check if the value isn't altered before it gets to the database.


Error inserting special characters (spanish) into database - El Forum - 03-12-2009

[eluser]jorgeakanieves[/eluser]
It´s not altered because I do this ...

Code:
$s = "INSERT INTO faqs_faqs(idfaq,title,question) VALUES ('','¿¿title??','¿¿cuestión??')";
$result = $this->db->query($s);
die;

In deed I did it too:
Code:
@mysql_query("USE prueba_borrar;INSERT INTO prueba(id,texto) VALUES ('','¿¿¿¿¿¿¿¿¿')");
die;



Error inserting special characters (spanish) into database - El Forum - 03-12-2009

[eluser]vitoco[/eluser]
Hi , if you set the charset to UTF8 in the table , you must first take this steps :

1.- save the .php file in UTF8
2.- encode de string to utf8 with utf8_encode

i have a helper with a function that encode the string only if it isn't utf8 ( utf8_encode_safe )

Code:
define("UTF_8", 1);
    define("ASCII", 2);
    define("ISO_8859_1", 3);

    function codificacion($texto)
    {
        $c        = 0;
        $ascii    = true;
        //
        for  ($i = 0 ; $i < strlen( $texto ) ; $i++ )
        {
            $byte = ord( $texto[$i] );
            //
            if( $c > 0 )
            {
                if( ( $byte>>6 ) != 0x2)
                {
                    return ISO_8859_1;
                }
                else
                {
                    $c--;
                }
            }
            elseif ($byte&0x80;)
            {
                $ascii = false;
                //
                if (($byte>>5) == 0x6)
                {
                    $c = 1;
                }
                elseif (($byte>>4) == 0xE)

                {
                    $c = 2;
                }
                elseif (($byte>>3) == 0x1E )
                {
                    $c = 3;
                }
                else
                {
                    return ISO_8859_1;
                }
            }
        }
        //
        return ($ascii) ? ASCII : UTF_8;
    }

    function utf8_encode_safe($texto)
    {
        return ( codificacion($texto) == ISO_8859_1) ? utf8_encode($texto) : $texto ;
    }

Saludos

NOTA : si eres hispanoparlante, te puedo dar una mejor explicacion , puesto que el ingles no es mi lengua materna


Error inserting special characters (spanish) into database - El Forum - 07-03-2009

[eluser]yayot[/eluser]
Hola, yo tengo una aplicacion que tiene los acentos en la base de datos, pero cuando lo despliego en el browser me saca caracteres raros donde deberian de ir los acentos?

Como puedo solucionarlo??

Gracias!


Error inserting special characters (spanish) into database - El Forum - 07-03-2009

[eluser]vitoco[/eluser]
[quote author="yayot" date="1246678642"]Hola, yo tengo una aplicacion que tiene los acentos en la base de datos, pero cuando lo despliego en el browser me saca caracteres raros donde deberian de ir los acentos?

Como puedo solucionarlo??

Gracias![/quote]
Primero debes verificar la codificacion de las tablas ,que pueden ser muchas, lo ideal sería "utf8 general ci".
Una vez verificado eso es recomendable que codifiques tu pagina tambien en utf-8 , codificacion que admite todos los caracteres especiales como tildes, eñes, etc...

Saludos


Error inserting special characters (spanish) into database - El Forum - 07-04-2009

[eluser]JoostV[/eluser]
Also remember to set the header of your view to utf-8:
Code:
header('Content-type: text/html; charset=utf-8');

And include a content meta tag
Code:
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;



Error inserting special characters (spanish) into database - El Forum - 07-04-2009

[eluser]yayot[/eluser]
Wow EXCELENTE JoostV and thanks vitoco, finally is fix:

this was the final fix

header('Content-type: text/html; charset=utf-8');

with that everything work great.

there is another tread, with your permission I will post the solution there also.

Regards!


Error inserting special characters (spanish) into database - El Forum - 07-04-2009

[eluser]JoostV[/eluser]
Hi Yayot,

Of course, be my guest.

Summing it all up in a nutshell: if you want to succesfully create a utf-8 site do ALL of the following:
1. create your code in an editor using utf-8 encoding
2. set up your db to use utf-8 charset and utf-8 collation (if you database is not in utf-8 run 'alter database mydatabase charset=utf8;')
3. before executing any queries, run query 'SET NAMES 'utf8''
4. before output to screen, set response header using header('Content-type: text/xml; charset=utf-8');
5. include meta tag &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;

For future reference,if you ever need to create XML, use
Code:
header('Content-type: text/xml; charset=utf-8');

Browsers will now interpret your output as XML.