Part of the EllisLab Network
   
 
Error inserting special characters (spanish) into database
Posted: 12 March 2009 04:04 AM   [ Ignore ]  
Summer Student
Total Posts:  28
Joined  03-11-2008

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:

$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 ):

[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?

Profile
 
 
Posted: 12 March 2009 04:36 AM   [ Ignore ]   [ # 1 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  4839
Joined  07-14-2006

You could check if the value isn’t altered before it gets to the database.

Profile
 
 
Posted: 12 March 2009 04:53 AM   [ Ignore ]   [ # 2 ]  
Summer Student
Total Posts:  28
Joined  03-11-2008

It´s not altered because I do this ...

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

In deed I did it too:

@mysql_query("USE prueba_borrar;INSERT INTO prueba(id,texto) VALUES ('','¿¿¿¿¿¿¿¿¿')");
die;
Profile
 
 
Posted: 12 March 2009 07:39 AM   [ Ignore ]   [ # 3 ]  
Grad Student
Avatar
Rank
Total Posts:  64
Joined  12-20-2006

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 )

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

Profile
 
 
Posted: 03 July 2009 05:37 PM   [ Ignore ]   [ # 4 ]  
Summer Student
Avatar
Total Posts:  8
Joined  06-18-2009

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!

 Signature 

Gerardo Jaramillo
Web Aficionado

Profile
 
 
Posted: 03 July 2009 05:40 PM   [ Ignore ]   [ # 5 ]  
Grad Student
Avatar
Rank
Total Posts:  64
Joined  12-20-2006
yayot - 03 July 2009 05:37 PM

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!

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

Profile
 
 
Posted: 04 July 2009 01:40 AM   [ Ignore ]   [ # 6 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  284
Joined  01-07-2008

Also remember to set the header of your view to utf-8:

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

And include a content meta tag

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

Accent Interactive - web& media

http://twitter.com/joostvanveen

Image resizing, cropping an squaring library | XML generator library | Pagination automated library

Profile
 
 
Posted: 04 July 2009 10:51 AM   [ Ignore ]   [ # 7 ]  
Summer Student
Avatar
Total Posts:  8
Joined  06-18-2009

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!

 Signature 

Gerardo Jaramillo
Web Aficionado

Profile
 
 
Posted: 04 July 2009 01:04 PM   [ Ignore ]   [ # 8 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  284
Joined  01-07-2008

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 <meta http-equiv=“Content-Type” content=“text/html; charset=utf-8” />

For future reference,if you ever need to create XML, use

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

Browsers will now interpret your output as XML.

 Signature 

Accent Interactive - web& media

http://twitter.com/joostvanveen

Image resizing, cropping an squaring library | XML generator library | Pagination automated library

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 819, on March 11, 2010 11:15 AM
Total Registered Members: 120364 Total Logged-in Users: 27
Total Topics: 126489 Total Anonymous Users: 6
Total Replies: 665192 Total Guests: 305
Total Posts: 791681    
Members ( View Memberlist )