简体   繁体   English

Malayalam字体字符作为未知字符保存到mysql数据库中

[英]Malayalam font characters saving into the mysql database as unknown characters

When I try to insert a utf8 charcter into my mysql web server database (table is also having collation: utf8_general_ci) unknown junk characters are inserted into the db. 当我尝试将utf8字符插入到我的mysql Web服务器数据库中(表也具有排序规则:utf8_general_ci)时,未知的垃圾字符会插入到db中。 I'm trying to input this name ( ടോണി ) in malayalam language into mysql db using an html file and when this value is submitted on the text box and send to the server db, it appears as like this: ( ടോണി ). 我试图使用html文件以malayalam语言将此名称( ടോണി )输入到mysql db中,并且当此值在文本框上提交并发送到服务器db时,它看起来像这样:( ടൠ‹à ´£à´¿ )。 I have also added this line in my php file: 我还在我的php文件中添加了这一行:

mysql_query("SET NAMES 'utf8' COLLATE 'utf8_general_ci'",$con);

but still no use. 但仍然没有用。 The name is in malayalam. 名字在马拉雅拉姆语中。 But what I get from db is unknown language. 但是我从数据库得到的是未知语言。 How is that happening?? 这是怎么回事? I will show you my php files and html files: 我将向您展示我的php文件和html文件:

insert.php insert.php

<?php   
$con=mysqli_connect("localhost","username","password","db_name"); 
if (mysqli_connect_errno($con)) 
{ 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
} 
$txt= $_POST['ta']; // get the unicode text from a submit action. 
$unicodeText = $txt; 
mysql_query("SET NAMES 'utf8' COLLATE 'utf8_general_ci'",$con);
$cQry= "insert into doc_test (unicodeText) values ('".$unicodeText."')" ; 
if(!mysqli_query($con,$cQry))
{
    die('Error: ' . mysqli_error($con));
}
echo "1 record added";
mysqli_close($con);
?> 

This is my html file: 这是我的html文件:

<html>
<head>
<meta charset="UTF-8" />  
</head>
<body>

<form action="insert.php" method="post">
Name: <input type="text" name="ta">
<input type="submit">
</form>
</body>
</html>

when i run this on my server and enter a value in malayalam in the textbox, and press the submit button, the value will be inserted into the mysql db, but it is not the one which I had entered in the textbox. 当我在服务器上运行此命令,并在文本框中的malayalam中输入一个值,然后按Submit按钮时,该值将插入到mysql db中,但这不是我在文本框中输入的值。 Also there is a warning shown after the value is entered in the db. 在数据库中输入该值后,还会显示警告。 It is shown below: 如下图所示:

PHP Error Message

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/a6001661/public_html/malayalam/insert.php on line 9

1 record added

What is the possible reason for this warning?? 此警告的可能原因是什么? Can someone please correct me out. 有人可以纠正我。 Also suggest the ideas to enter the value in the correct font malayalam in db. 还建议在数据库中以正确的字体malayalam输入值的想法。 Thanks in advance... 提前致谢...

I've decided to make my comment as an answer, for possible future readers and to close the question. 我已决定将我的评论作为答案,以供将来的潜在读者阅读并结束问题。


Try placing $con->set_charset("utf8"); 尝试放置$con->set_charset("utf8"); right before your query. 在您查询之前。

That is one method that has worked for many here on SO, where a similar problem was faced. 这是在SO上对许多人都适用的一种方法,在那里也遇到了类似的问题。

Also, add error reporting to the top of your file(s) after your opening <?php tag: 另外,在打开<?php标记后,将错误报告添加到文件顶部:

error_reporting(E_ALL);
ini_set('display_errors', 1);

Sidenote: You are presently mixing MySQL APIs using mysql_query() . 旁注:您目前正在使用mysql_query()混合MySQL API。

It does not mix with mysqli_* functions inside the same code. 它不会与同一代码中的mysqli_*函数混合使用。 It would need to be mysqli_query() 它需要是mysqli_query()


Edit: 编辑:

"Success.. Adding <meta charset="utf-8"> solved my problem" “成功。添加<meta charset="utf-8">解决了我的问题”

as per comments below. 按照下面的评论。

You can get this in three simple steps. 您可以通过三个简单的步骤来完成此操作。

  1. simply set column Collation to "utf8_general_ci" in your database 只需在数据库中将“排序规则”列设置为“ utf8_general_ci”

  2. header('Content-type: text/html; charset=UTF-8'); header('Content-type:text / html; charset = UTF-8'); in your file. 在您的文件中。

and

  1. $mysqli->set_charset("utf8"); $ mysqli-> set_charset( “UTF8”); this after mysql connect query. mysql连接查询后。

thats all. 就这样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM