I would like to know how could I enconde russian/hindi/chinese... characters into utf8. I know about the functions utf8_decode/utf8_encode, but they only work with ISO-8859-1.
Is there in php a more generic function that do they same task with any kind of characters?? which one should I use if I want to encode/decode russian characters??
I have also tried this: mb_convert_encoding($string, 'UTF-8', 'CP1251');
But it doesnt work, it converts: Екатеринка in Екатеринка
EDIT:
The script I'm using is a very simple form the user has to fill to store some information into a database:
<?php header('Content-Type: text/html; charset=utf-8'); //To specify to the browser the kind of content
$con = mysql_connect('**host**', '**user**', '**pass**');
mysql_select_db('encoding_test', $con);
mysql_set_charset('utf8', $con);
if($_POST['submitted']){
//<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> this should go up
$name2= $_POST['name'];
echo $name2."<br>";
/* The name I'm inserting in the form is the following one
$name="Екатеринка";
*/
require_once('mysqli_connect.php');
$q="INSERT INTO USERS (name,pass) VALUES ('$name2' ,'pass')";
$r = @mysqli_query($dbc, $q); //Here we run the query
if($r)
{
echo 'Everything OK '.$q.'<br>';
}else{
echo 'Something wrong<br>';
echo '<p>'.mysqli_error($dbc).'<br /><br />Query:'.$q.'</p>';
}
}//Lo del submmited que hay que elminar al final de las pruebas
?>
<html xml:lang=en ""lang="en">
<head>
<title>Register From</title>
<h1>Register From</h1>
</head>
<body>
<form action="Main_menu.php" accept-charset="utf-8" method="post">
<p>First Name: <input type="text" name="name" size="15" maxlength="20" /></p>
<p>Password: <input type="password" name="pass" size="15" maxlength="20" /></p>
<p><input type="submit" name="submit" value="Register" /></p>
<input type="hidden" name="submitted" value="TRUE" />
</form>
</body>
</html>
The code to create the table in the database is:
require_once('../mysqli_connect.php');
//We create now the USERS table
$q="CREATE TABLE USERS(user_id
MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT, name VARCHAR(30) NOT
NULL, email VARCHAR(80) NOT NULL, pass VARCHAR(30) NOT
NULL, PRIMARY KEY (user_id)) ENGINE=MyISAM DEFAULT CHARSET=utf8";
$r = @mysqli_query($dbc, $q); //Here we run the query
When I execute the scrip, everything goes fine, it connects with the database and the displayed message is:
Everything OK INSERT INTO USERS (name) VALUES ('Екатеринка')
But then I go to the database and the information stored is: Екатеринка... But if I copy&paste in the SQL prompt of the data base "Everything OK INSERT INTO USERS (name) VALUES ('Екатеринка')" and press enter, the information stored is Екатеринка.
Before reading dezece's post: kunststube.net/frontback. I think the problem is not in my script, because cyrillic characters are displayed correctly, is not in the database because they are stored correctly if I use it's own SQL prompt, so the problem has to be in the connection between the browser and the database.
Should I try something else besides:
$con = mysql_connect('**host**', '**user**', '**pass**');
mysql_select_db('encoding_test', $con);
mysql_set_charset('utf8', $con);
**In my script I'm using the real information
EDIT 2:
Well I added a few lines more, just to check how the information is retrieved from the data base:
$q="SELECT name FROM USERS WHERE pass='pass'";
$r=@mysqli_query($dbc, $q);
$row=mysqli_fetch_array($r, MYSQLI_ASSOC);
echo "We get from the Database: ".$row['name']."<br>";
And the result was: "We get from the Database: Екатеринка"
So despite the information is stored wrongly in the table, at least it can be retrieved nicely