Unable to encode characters in PHP (Incorrect string value)

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

I try 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, whenever I try to send the string 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 datatype of the fields in the table are UTF-8 (utf8_unicode_ci), so I must encode my string using the same

This thread is related to my question: Detect encoding and make everything UTF-8.

However, I need to know how to encode any character to UTF-8, before inserting it to the database, otherwise, I get an error as the one aforementioned. I think I need to identify first what kind of characters I am receiving before putting them into database.

Answers


utf8_unicode_ci is the collation, you need the character set as utf8 as well:

CREATE TABLE someTable DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_unicode_ci;

For good measure, make sure that everything is 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 on the Hebrew gibberish translation website I run, and it handles everything that myself or that users throw at it.


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

I only get this error when i use strtolower

My database and collation are all UTF-8 and have no problems with characters across the board. I have a language database table that handles Japanese, Russian, Chinese etc. without any issues. This error only popped up when i tried strtolower

You need to use PHP's mb_strtolower when working with uni-code characters


Need Your Help

Trying to trace the time it takes for my website hosted in New Zealand to be accessed from the US

http proxy traceroute

I live in New Zealand and I just started a website which I also have hosted in New Zealand as novazeal.com and novazeal.co.nz. I am hoping to target clients overseas though as well as in New Zealan...

Making website work offline with data sharing between LAN connected systems

javascript html5 storage offlineapps offline-browsing

HTML5 supports online web storage which can help in making our website to work offline. But, how can one share data between systems that are connected through LAN when offline?