开发者

inserting ô into mysql database is part of Rhône results in Rh

When I insert the string 'Rhône' into my database, all that remains is 'Rh' How is that possible? Is use Mysql with utf8_general_ci encoding.

<?php
header( 'Content-Type: text/html; charset=utf-8' );
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-gb" lang="en-gb">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>Decode</title>
<head></head>
<body>
<?php
include_once ($_SERVER['DOCUMENT_ROOT'] . '/v3/functions/connect.php');

$res_id = $_POST['res_id'];
$yr_id = $_POST['yr_id'];

$yr_id=html_entity_decode($yr_id, ENT_COMPAT, "UTF-8");


 if(isset($yr_id) && $yr_id<>'') {

$search  = array('%C3%B4');
$replace = array('ô');
$yr_id=str_replace($search, $replace, $yr_id);



 mysql_query("SET NAMES utf8"开发者_StackOverflow);
   $uQuery2="UPDATE sv_snowrepublic set yr_id='$yr_id' where res_id=$res_id limit 1";
  $uResult2  = mysql_query($uQuery2);
  echo 'updated';

  } 







  $uQuery1="SELECT * FROM sv_snowrepublic where res_id=$res_id limit 1";
  $uResult1  = mysql_query($uQuery1);
  $row1 = mysql_fetch_assoc($uResult1);


if (mysql_num_rows($uResult1) >= 1 && isset($res_id) && $res_id>2)

     {
echo "<form method='POST' action='index.php'>";
echo "<input type='hidden' size='4' value='" . $row1['res_id'] . "' name='res_id'  /><br />";

$yr2_id=$row1['yr_id'];
// $yr2_id=html_entity_encode($yr2_id, ENT_COMPAT, "UTF-8");
echo mb_detect_encoding($yr2_id);

echo "<input type='text' size='200' value='" . $yr2_id . "' name='yr_id'  /><br />";
echo "<br /><label for='submit'></label><input  type='submit' name='submit' value=' Send '></form>";
        }       


?>

</body>
</html>


How is that possible?

You have got a problem to transfering the string Rhône into your MySQL database. This can have multiple reasons, most certainly one of the components involved (most certainly MySQL as server or client) runs over a string with an encoding specified which does not match at the character position you commonly refer to as ô. As it's the first position the encoding looks invalid, it will get cut off.

You must ensure, under all circumstance and without making any error - not even the slightest one in configuration - that the encoding of the data you handle is always clear to you, correctly set-up and properly handled.

That's both for the input into your database as well as for getting the data from your database.

The thing that comes to mind is:

  • You don't save your php file encoded as UTF-8 to disc.

Further on:

  • You're not using the recommended MySQL client extension.
  • You're using SET NAMES instead of setting up the connection encoding settings properly. See mysql_set_charsetDocs.

Check that, but it's only a guess, there can be many reasons why this happens.


you should set connection encoding to UTF-8 too. Try

SET NAMES UTF-8

after connection, before first query


If you are using mysql_* functions, you need to make sure you escape the string with mysql_real_escape_string(), however it is recommended to use prepared statements using PDO or mysqli.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜