0

I need to save text with language specific caracters in a mysql database. I have setup the table and the field with utf8_general_ci enter image description here

The test.php file to write into the database is also in utf8_general_ci:

<?php 

include 'connect.php';

$x=$_GET["x"];

$d="INSERT INTO `test` 
(`car`) 
VALUES (' $x ')";

echo $d;
$resultins = mysql_query($d);

echo mysql_error();
echo "<br>".$id;
?>

In the browser I do:

/test.php?x=Vietnam_ờ_French_ç_German_ä

and the echo gives: INSERT INTO test (car) VALUES (' Vietnam_á»_French_ç_German_ä ')

In database I get: enter image description here

When I retrieve this data in another php and display it in the browserI get: enter image description here

What am I missing?

Gumbo
  • 643,351
  • 109
  • 780
  • 844
michaelsmith
  • 1,011
  • 1
  • 16
  • 35
  • **Danger**: You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are also **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Jan 10 '15 at 10:56

2 Answers2

1

Maybe before you start the select query, you should set the queries up to return UTF8 encoded string. First, use a <meta charset="utf-8"> in the head and before the query try this: mysqli_query($connection, "SET NAMES UTF8");. Hope it helps.

Krisztián Dudás
  • 856
  • 10
  • 22
  • does the trick. Thank you! – michaelsmith Jan 10 '15 at 11:56
  • I accepted answer too quickly. With the meta the echo is showing correctly, but the database is still wrong. When I add $connection->set_charset('utf8'); I get a Fatal error: Call to a member function set_charset() on a non-object – michaelsmith Jan 10 '15 at 12:21
  • @michaelsmith What is the database character encoding? If it's in utf8_general_ci, it takes any kind of character. – Krisztián Dudás Jan 12 '15 at 11:47
-1

Place $dbc->set-charset('utf8') before any query.

Sonia
  • 172
  • 3
  • 9