无法在mysql数据库中正确插入希腊字符 [英] Cannot properly insert greek characters in mysql database

查看:174
本文介绍了无法在mysql数据库中正确插入希腊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在从模拟器向mysql数据库发送数据时,我们的mysql数据库显示了ÎÎÎλÏÏÏÏÏÏÏÏÏÏ代替希腊字符。



来自phpMyAdmin的萤幕撷取画面:





更新:



使用

之后

@Félix Gagnon-Grenier在我的代码中回答它给我这个:





Sql for table creation

  CREATE TABLE`cart`(
`id` bigint(20)NOT NULL AUTO_INCREMENT,
`product_name` varchar(255)NOT NULL,
`product_price` double(3,2)NOT NULL,
` `int(11)NOT NULL,
`preferation1` varchar(50)NOT NULL,
`preferation2` varchar(50)NOT NULL,
`preferation3` varchar
`preferation4` varchar(50)NOT NULL,
`magazi_id` int(11)NOT NULL,
`servitoros_id` int(11)NOT NULL,
PRIMARY KEY id`)
)ENGINE = InnoDB AUTO_INCREMENT = 7 DEFAULT CHARSET = utf8



php

  ;?php 
error_reporting(E_ALL ^ E_NOTICE);?
ini_set(default_charset,UTF-8);
header('Content-type:text / html; charset = UTF-8');
mb_internal_encoding('UTF-8');
mb_http_input(utf-8);
try {
$ handler = new PDO('mysql:host = localhost; dbname = database','username','password');
$ handler-> setAttribute(PDO :: MYSQL_ATTR_INIT_COMMAND,SET NAMES'utf8'COLLATE'utf8_general_ci');
$ handler-> setAttribute(PDO :: ATTR_ERRMODE,PDO :: ERRMODE_EXCEPTION);
} catch(Exception $ e){
echo $ e-> getMessage();
die();
}

$ productName = $ _POST ['productName'];
$ productPrice = $ _ POST ['productPrice'];
$ productImage = $ _POST ['productImage'];
$ quantity = $ _POST ['quantity'];
$ sugar = $ _POST ['sugar'];
$ milk = $ _POST ['milk'];
$ flavor = $ _ POST ['flavor'];
$ comment = $ _POST ['comment'];
$ magazi = $ _POST ['magazi_id'];
$ servitoros = $ _POST ['servitoros_id'];

$ handler-> query(INSERT INTO cart(id,product_name,product_price,product_image,quantity,preferation1,preferation2,preferation3,preferation4,magazi_id,servitoros_id)VALUES('','$ productName ','$ productPrice','$ productImage','$ quantity','$ sugar','$ milk','$ flavor','$ comment','$ magazi','$ servitoros')
die();
?>

Java

  protected Void doInBackground(String ... params){
nameValuePairs = new ArrayList<>();
nameValuePairs.add(new BasicNameValuePair(productName,productName));
nameValuePairs.add(new BasicNameValuePair(productPrice,String.valueOf(price)));
nameValuePairs.add(new BasicNameValuePair(productImage,image));
nameValuePairs.add(new BasicNameValuePair(quantity,String.valueOf(quantityNumberFinal)));
nameValuePairs.add(new BasicNameValuePair(sugar,sugarPreference));
nameValuePairs.add(new BasicNameValuePair(milk,milkPreference));
nameValuePairs.add(new BasicNameValuePair(flavor,flavorPreference));
nameValuePairs.add(new BasicNameValuePair(comment,comment));
nameValuePairs.add(new BasicNameValuePair(magazi_id,String.valueOf(2)));
nameValuePairs.add(new BasicNameValuePair(servitoros_id,String.valueOf(13)));
try
{
HttpParams httpParams = new BasicHttpParams();
HttpProtocolParams.setContentCharset(httpParams,UTF-8);
httpClient = new DefaultHttpClient(httpParams);
httpPost = new HttpPost(params [0]);
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs,UTF-8));
response = httpClient.execute(httpPost);
httpEntity = response.getEntity();
is = httpEntity.getContent();
}
catch(Exception e)
{
Log.e(Fail 1,e.toString());
}
return null;
}


解决方案

字符集编码。重要的是,您的整个代码具有相同的字符集,以避免字符显示不正确的问题。



有很多设置需要正确定义,我强烈推荐使用UTF-8,因为这里有大多数字母你需要(斯堪的纳维亚语,希腊语,阿拉伯)。



这里有一些必须设置为特定字符集的事情。



标题




  • 将HTML和PHP标头中的字符集设置为UTF-8




    • PHP: header('Content-Type:text / html; charset = utf-8');

      (PHP标头必须放在任何输出之前(echo,whitespace,
      HTML)!)


    • HTML:< meta http-equiv =Content-Typecontent =text / html; charset = utf-8/> br>
      (HTML标题放置在< head> / < / head> 标签)





/ p>


  • 您还需要在连接本身中指定字符集。对于你的PDO示例,它是这样做的



    $ handler = new PDO('mysql:host = localhost; dbname = database; charset = utf8 ','username','password',array(PDO :: MYSQL_ATTR_INIT_COMMAND =>SET CHARACTER SET UTF8));



    注意 charset = utf8 -attribute。其他MySQL-API有不同的方法,如果你在未来使用别的东西。


  • 您的数据库及其表必须设置为UTF-8。请注意,字符集不是与排序规则相同。我看到你已经将你的排序规则设置为UTF-8,所以这是好的,但对整个数据库和所有表也一样。



    你可以通过运行



    ALTER DATABASE databasename CHARACTER SET utf8 COLLATE utf8_unicode_ci;

    ALTER TABLE tablename CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;




php.ini规格




  • php.ini 文件,您应该为您的平台指定默认字符集,例如 default_charset =utf-8;



文件编码




  • 同样重要的是, .php 文件本身是UTF-8编码的。如果你使用Notepad ++编写你的代码,这可以在任务栏的格式下拉列表中完成。



我不太了解Java,但是如果你也可以将属性设置为UTF-8。实质上,可以设置为特定字符集的所有内容应设置为相同。



如果你遵循上面的所有指针,很有可能你的问题会得到解决。如果没有,您可以查看此StackOverflow帖子: UTF-8一路通过


Our mysql database shows Î Î¿Î»Ï Î³Î»Ï…ÎºÏŒÏ in place of greek characters while sending data from an emulator to a mysql database. Other characters are left ok.

screenshot from phpMyAdmin:

UPDATE:

After using

@Félix Gagnon-Grenier answer in my code it gives me this:

Sql for table creation

CREATE TABLE `cart` (
 `id` bigint(20) NOT NULL AUTO_INCREMENT,
 `product_name` varchar(255) NOT NULL,
 `product_price` double(3,2) NOT NULL,
 `product_image` varchar(255) NOT NULL,
 `quantity` int(11) NOT NULL,
 `preferation1` varchar(50) NOT NULL,
 `preferation2` varchar(50) NOT NULL,
 `preferation3` varchar(50) NOT NULL,
 `preferation4` varchar(50) NOT NULL,
 `magazi_id` int(11) NOT NULL,
 `servitoros_id` int(11) NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8

php

<?php
    error_reporting(E_ALL ^ E_NOTICE);
    ini_set("default_charset", "UTF-8");
    header('Content-type: text/html; charset=UTF-8');
    mb_internal_encoding('UTF-8');
    mb_http_input("utf-8");
    try {
        $handler = new PDO('mysql:host=localhost;dbname=database', 'username', 'password');
        $handler->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES 'utf8' COLLATE 'utf8_general_ci' ");
        $handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    } catch (Exception $e) {
        echo $e->getMessage();
        die();
    }

    $productName = $_POST['productName'];
    $productPrice=$_POST['productPrice'];
    $productImage = $_POST['productImage'];
    $quantity = $_POST['quantity'];
    $sugar = $_POST['sugar'];
    $milk = $_POST['milk'];
    $flavor=$_POST['flavor'];
    $comment = $_POST['comment'];
    $magazi = $_POST['magazi_id'];
    $servitoros = $_POST['servitoros_id'];

    $handler->query("INSERT INTO cart(id, product_name, product_price, product_image, quantity, preferation1, preferation2, preferation3, preferation4, magazi_id, servitoros_id) VALUES('', '$productName','$productPrice','$productImage', '$quantity', '$sugar', '$milk', '$flavor', '$comment', '$magazi', '$servitoros')");
    die();
?>

Java

protected Void doInBackground(String... params) {
            nameValuePairs = new ArrayList<>();
            nameValuePairs.add(new BasicNameValuePair("productName", productName));
            nameValuePairs.add(new BasicNameValuePair("productPrice", String.valueOf(price)));
            nameValuePairs.add(new BasicNameValuePair("productImage", image));
            nameValuePairs.add(new BasicNameValuePair("quantity", String.valueOf(quantityNumberFinal)));
            nameValuePairs.add(new BasicNameValuePair("sugar", sugarPreference));
            nameValuePairs.add(new BasicNameValuePair("milk", milkPreference));
            nameValuePairs.add(new BasicNameValuePair("flavor", flavorPreference));
            nameValuePairs.add(new BasicNameValuePair("comment", comment));
            nameValuePairs.add(new BasicNameValuePair("magazi_id", String.valueOf(2)));
            nameValuePairs.add(new BasicNameValuePair("servitoros_id", String.valueOf(13)));
            try
            {
                HttpParams httpParams = new BasicHttpParams();
                HttpProtocolParams.setContentCharset(httpParams, "UTF-8");
                httpClient = new DefaultHttpClient(httpParams);
                httpPost = new HttpPost(params[0]);
                httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
                response = httpClient.execute(httpPost);
                httpEntity = response.getEntity();
                is = httpEntity.getContent();
            }
            catch(Exception e)
            {
                Log.e("Fail 1", e.toString());
            }
            return null;
        }

解决方案

Your problem is related to your charset-encoding. It's important that your entire code has the same charset to avoid issues where characters displays incorrectly.

There are quite a few settings that needs to be properly defined and I'd strongly recommend UTF-8, as this has most letters you would need (Scandinavian, Greek, Arabic).

Here's a little list of things that has to be set to a specific charset.

Headers

  • Setting the charset in both HTML and PHP headers to UTF-8

    • PHP: header('Content-Type: text/html; charset=utf-8');
      (PHP headers has to be placed before any output (echo, whitespace, HTML)!)

    • HTML: <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      (HTML-headers are placed within the <head> / </head> tag)

Connection

  • You also need to specify the charset in the connection itself. For your PDO example, it's done like this

    $handler = new PDO('mysql:host=localhost;dbname=database;charset=utf8', 'username', 'password', array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET CHARACTER SET UTF8"));

    Note the charset=utf8-attribute. Other MySQL-APIs have different ways of doing this should you use something else in the future.

Database

  • Your database and its tables has to be set to UTF-8. Note that charset is not the same as collation. I see you already set your collation to UTF-8, so that's good, but do the same for the entire database and all tables.

    You can do that by running the queries below once for each database and tables (for example in phpMyAdmin)

    ALTER DATABASE databasename CHARACTER SET utf8 COLLATE utf8_unicode_ci;
    ALTER TABLE tablename CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;

php.ini specification

  • In your php.ini file, you should specify the default charset for your platform, like this default_charset = "utf-8";

File-encoding

  • It's also important that the .php file itself is UTF-8 encoded. If you're using Notepad++ to write your code, this can be done in the "Format" drop-down on the taskbar.

I don't know much about Java, but if you can set attributes to UTF-8 there as well, do it. In essence, everything that can be set to a specific charset should be set to the same.

Should you follow all of the pointers above, chances are your problem will be solved. If not, you can take a look at this StackOverflow post: UTF-8 all the way through.

这篇关于无法在mysql数据库中正确插入希腊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆