无法在mysql数据库中正确插入希腊字符

5 php mysql android character-encoding http-headers

我们的mysql数据库显示Î Î¿Î»Ï Î³Î»Ï…ÎºÏŒÏ代替希腊字符,同时将数据从模拟器发送到mysql数据库.其他字符保持不变.

phpMyAdmin截图:

数据

更新:

使用后

@FélixGagnon-Grenier在我的代码中回答它给了我:

数据库

用于表创建的SQL

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
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

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();
?>
Run Code Online (Sandbox Code Playgroud)

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;
        }
Run Code Online (Sandbox Code Playgroud)

Qir*_*rel 6

您的问题与您的charset编码有关.重要的是,你的整个代码具有相同的字符集,以避免出现问题,其中的字符显示不正确.

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

这里有一些必须设置为特定字符集的事项列表.

连接

  • 您还需要在连接本身中指定charset .对于您的PDO示例,它就像这样完成

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

    注意charset=utf8-attribute.如果您将来使用其他东西,其他MySQL-API有不同的方法.

数据库

  • 您的数据库及其表必须设置为UTF-8.需要注意的是,charset是一样的排序规则.我看到你已经将你的校对设置为UTF-8,所以这很好,但对整个数据库和所有表都做同样的事情.

    你可以通过为每个数据库和表运行一次以下的查询来做到这一点(例如在phpMyAdmin中)

    ALTER DATABASE databasename CHARACTER SET utf8 COLLATE utf8_unicode_ci; 
    ALTER TABLE tablename CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;
    
    Run Code Online (Sandbox Code Playgroud)

    请注意,已存储在数据库中的任何数据都不会自动修复其损坏的字符集.因此,在插入数据之前执行此操作非常重要,或者在设置字符集后重新插入数据.

php.ini规范

文件编码

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

表情符号

  • 在MySQL(表,数据库和连接对象)中,如果您希望使用emojis,则需要指定utf8mb4charset,而不是常规utf8.

我对Java知之甚少,但是如果你也可以将属性设置为UTF-8,那就去做吧.实质上,可以设置为特定字符集的所有内容都应设置为相同.

如果你按照上面的所有指示,你的问题很可能会得到解决.如果没有,你可以看看这个StackOverflow帖子:UTF-8一直到.


luk*_*sch 1

在此博客中可以找到有关在 PHP 和 Mysql 中使用 UTF8 的精彩介绍。要点是:

  1. 将数据库表设置为UTF8
  2. 在你的 php.ini 设置中default_charset = "utf-8";
  3. 建立连接后,您可能必须运行以下命令/查询:set names UTF-8;

我猜你忽略了第3点。

在你的 PHP 脚本中,你应该做这样的事情(未经测试):

$handler = new PDO('mysql:host=localhost;dbname=database', 
  'username', 
  'password',
  array(PDO::MYSQL_ATTR_INIT_COMMAND => 
    'SET NAMES utf8;SET CHARACTER SET UTF8;'));
Run Code Online (Sandbox Code Playgroud)


归档时间:

查看次数:

6596 次

最近记录:

8 年,7 月 前