不能正确地插入mysql数据库希腊字符 [英] Cannot properly insert greek characters in mysql database

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

问题描述

我们的mysql数据库显示 IοÎ我γλ我......κόÏ代替希腊字母,而从模拟器到MySQL数据库发送数据。其他字符留下ok了。

从phpMyAdmin的截图:

数据

更新:

使用后

@费利克斯·加侬 - 格尼尔回答我的code它给了我这样的:

数据库

SQL中为表创建

  CREATE TABLE`cart`(
 `id` BIGINT(20)NOT NULL AUTO_INCREMENT,
 `product_name` VARCHAR(255)NOT NULL,
 `双product_price`(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默认字符集= UTF8

在这里输入的形象描述

PHP

 < PHP
    使用error_reporting(E_ALL ^​​ E_NOTICE);
    的ini_set(DEFAULT_CHARSET,UTF-8);
    标题(内容类型:text / html的;字符集= UTF-8');
    mb_internal_encoding(UTF-8);
    mb_http_input(UTF-8);
    尝试{
        $处理=新PDO('MySQL的:主机=本地主机,数据库名=数据库,用户名,密码);
        $ handler->的setAttribute(PDO :: MYSQL_ATTR_INIT_COMMAND,SET NAMES'UTF8'COLLATE'utf8_general_ci');
        $ handler->的setAttribute(PDO :: ATTR_ERRMODE,PDO :: ERRMODE_EXCEPTION);
    }赶上(例外$ E){
        回声$ E->的getMessage();
        死();
    }    $产品名称= $ _ POST ['产品名称'];
    $ productSku,则= $ _ POST ['productSku,则'];
    $ productImage = $ _ POST ['productImage'];
    $数量= $ _ POST ['量'];
    $ =糖$ _POST ['糖'];
    $牛奶= $ _ POST ['牛奶'];
    $味道= $ _ POST ['味'];
    $评论= $ _ POST ['评论'];
    $ magazi = $ _ POST ['magazi_id'];
    $ servitoros = $ _ POST ['servitoros_id'];    $ handler->查询(INSERT INTO车(ID,PRODUCT_NAME,PRODUCT_PRICE,product_image,数量,preferation1,preferation2,preferation3,preferation4,magazi_id,servitoros_id)VALUES('' ,'$产品名称,$ productSku,则,$ productImage','$数量,$糖,$牛奶','$味','$评论,$ magazi','$ servitoros ));
    死();
?>

Java的

 保护无效doInBackground(字符串... PARAMS){
            namevaluepairs中=新的ArrayList<>();
            nameValuePairs.add(新BasicNameValuePair(产品名称,产品名称));
            nameValuePairs.add(新BasicNameValuePair(productSku,则,将String.valueOf(价格)));
            nameValuePairs.add(新BasicNameValuePair(productImage图片));
            nameValuePairs.add(新BasicNameValuePair(量,将String.valueOf(quantityNumberFinal)));
            nameValuePairs.add(新BasicNameValuePair(糖,糖preference));
            nameValuePairs.add(新BasicNameValuePair(牛奶,牛奶preference));
            nameValuePairs.add(新BasicNameValuePair(味道,味道preference));
            nameValuePairs.add(新BasicNameValuePair(注释,评论));
            nameValuePairs.add(新BasicNameValuePair(magazi_id,将String.valueOf(2)));
            nameValuePairs.add(新BasicNameValuePair(servitoros_id,将String.valueOf(13)));
            尝试
            {
                的HttpParams的HttpParams =新BasicHttpParams();
                HttpProtocolParams.setContentCharset(的HttpParams,UTF-8);
                HttpClient的=新DefaultHttpClient(的HttpParams);
                httpPost =新HttpPost(PARAMS [0]);
                httpPost.setEntity(新UrlEn codedFormEntity(namevaluepairs中,UTF-8));
                响应= httpClient.execute(httpPost);
                httpEntity = response.getEntity();
                是= httpEntity.getContent();
            }
            赶上(例外五)
            {
                Log.e(故障1,e.toString());
            }
            返回null;
        }


解决方案

您的问题是涉及到字符集编码的。重要的是,你的全部的code具有相同的字符集,以避免出现问题,其中的字符显示不正确。

有相当需要被正确定义一些设置,我会强烈推荐UTF-8,因为这最有字母,你需要(斯堪的纳维亚,希腊语,阿拉伯语)。

下面是一个已经被设置为一个特定的字符集的事情有点列表。

标题


  • 在HTML和PHP头的字符集设置为UTF-8


    • PHP:头('内容类型:text / html的;字符集= utf-8'); 结果
      (PHP头已被放置之前的任何的输出(回声,空白,
      HTML)!)


    • HTML:&LT; META HTTP-EQUIV =Content-Type的CONTENT =text / html的;字符集= UTF-8/&GT; < BR>
      (HTML的头被放置在&LT; HEAD&GT; / &LT; /头&GT; 标记)



连接


  • 您还需要在连接本身来指定字符集的。为了您的PDO为例,它的完成这样的

    $处理=新PDO('MySQL的:主机=本地主机,数据库名=数据库;字符集= UTF8,用户名,密码,阵列(PDO :: MYSQL_ATTR_INIT_COMMAND =&gt;中SET CHARACTER SET UTF8));

    请注意在的charset = UTF8 -attribute。其他MySQL的API中有你应该使用的东西在将来别的做这个的不同方式。


数据库


  • 您的数据库的和它的表必须设置为UTF-8。需要注意的是,charset是的的相同的排序规则。我看你已经设置的排序规则以UTF-8,所以这是很好的,但做整个数据库和所有表一样的。

    您可以做到这一点通过为每个数据库和表(例如在phpMyAdmin)

    运行下面的查询一次

    ALTER DATABASE数据库名称CHARACTER SET UTF8 COLLATE utf8_uni code_ci; 结果
    ALTER TABLE表名转换为字符集UTF8 COLLATE utf8_uni code_ci;


php.ini文件规范


  • 的php.ini 文件时,应指定默认字符集为您的平台,这样的 DEFAULT_CHARSET =UTF-8;

文件编码


  • 同样重要的是,的.php 文件本身是UTF-8 EN codeD。如果您使用记事本++写您的code,这可以在格式任务栏上的下拉菜单来完成。

我不知道很多关于Java,但如果你可以设置属性为UTF-8有作为,做到这一点。在本质上,这可以被设置为特定的字符集一切应设置为相同。

如果你按照上面的所有指针,机会是你的问题会得到解决。如果没有,你可以看看这个计算器后: 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天全站免登陆