Cannot encode characters in PHP (invalid string value)

I want to encode Danish characters before sending them to the database.

I am trying to apply this function to them:

private function process_array_elements(&$element){ $element = utf8_encode($element); $element = strip_tags( $element ); $element = strtolower ( trim ( $element ) ); $element = mysql_real_escape_string($element); return $element; } 

Like this:

 $this->check_line_two = $this->process_array_elements($e); 

Now when I try to send a row to the database:

 mysql_query("SET NAMES 'utf8'"); $query="INSERT INTO result_scanned SET line_one= '$this->check_line_one', line_two='$this->check_line_two', line_three='$this->check_line_three', advert_id='$this->advert_id', scanned='$this->scan_result'"; 

I get this:

  Incorrect string value: '\xE3\x83\xE2\xB8r ...' for column 'line_three' at row 1 

The data type of the fields in the table is UTF-8 ( utf8_unicode_ci ), so I have to encode my string using the same

This thread relates to my question: Detect encoding and do all UTF-8 .

However, I need to know how to encode any character in UTF-8 before inserting it into the database, otherwise I get an error message as above. I think I need to first determine which characters I get before putting them into the database.

+4
source share
2 answers

utf8_unicode_ci is a sort, you need a character set like utf8 :

 CREATE TABLE someTable DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_unicode_ci; 

For a good measure, make sure that all UTF-8 when connecting to MySQL with PHP:

 mysql_connect ("localhost", "DB_USER", "DB_PASSWORD") or die (mysql_error()); mysql_select_db ("DATABASE_NAME") or die (mysql_error()); mysql_query("SET character_set_client=utf8"); mysql_query("SET character_set_connection=utf8"); mysql_query("SET character_set_database=utf8"); mysql_query("SET character_set_results=utf8"); mysql_query("SET character_set_server=utf8"); mysql_query("SET NAMES 'utf8'"); 

This is how I connect to the Hebrew translation of the reincarnation that I run and processes everything that users themselves or that users throw on it.

+4
source
 Incorrect string value: '\xE3\x83\xE2\xB8r ...' for column 

I get this error only when using strtolower

My database and sorting are all UTF-8 and have no problems with characters in all directions. I have a language database table that handles Japanese, Russian, Chinese, etc. Without any problems. This error appeared only when I tried strtolower

You need to use PHP mb_strtolower when working with unicode characters

0
source

All Articles