简体   繁体   English

MySQL到php charset问题

[英]MySQL to php charset issue

I suppose it's common trouble, so, what i have: DB schema with table, containing INTs and VARCHARS of utf8 - ut8_unicode_ci, and this: 我想这是常见的麻烦,所以,我有什么:带有表的数据库模式,包含INTs和utf8的VARCHARS - ut8_unicode_ci,以及:

<?php
$hostname = "localhost";
$username = "root";
$password = "";
$dbname = "jdmdb";
?>
<html>
<head> 
<meta http-equiv="Content-Tуpe" сontent="tехt/html; charset=utf-8" />
    <title>JDM Database</title>
</head>
<body>
<?
    mysql_query("SET NAMES utf8");
    $connection = mysql_connect($hostname, $username, $password) or     die("Не удаётся подключиться: </br>". mysql_error());
    mysql_select_db($dbname, $connection) or die("хуй: </br>".          mysql_error());

    $query = ('select * from cars where id="'.$_GET['c1'].'";');
    $result = mysql_query($query) or die(mysql_error());

    ?>
    <table border=1><tr>
    <?
        while ($result_row = mysql_fetch_row(($result))){
        echo '<td>'.$result_row[1].'</td><td>'.$result_row[2].'</td><td>'.$result_row[3].'</td><td>'.$result_row[4].'</td><td>'.$result_row[5].'</td><td>'.$result_row[6].'</td><td>'.$result_row[7].'</td><td>'.$result_row[8].'</td><td>'.$result_row[9].'</td><td>'.$result_row[10].'</td><td>'.$result_row[11].'</td><td>'.$result_row[12].'</td><td>'.$result_row[13].'</td><td>'.$result_row[14].'</td><td>'.$result_row[15].'</td><td>'.$result_row[16].'</td><td>'.$result_row[17].'</td>';
            echo '</tr>'; 
    }?>
    </table>
    <?
    mysql_close($connection);
    ?>
    </body>
</html>

and what i recieve: 我接受了什么:

在此输入图像描述

Any suggestions? 有什么建议么?

There are several ways to set character to utf-8. 有几种方法可以将字符设置为utf-8。

  1. While creating the table with phpMyAdmin, select utf8_unicode_ci 使用phpMyAdmin创建表时,请选择utf8_unicode_ci

  2. Add mysqli_query("SET NAMES 'UTF8′") when you're accessing the database. 在访问数据库时添加mysqli_query("SET NAMES 'UTF8′")

  3. Set utf-8 in html tag: meta charset="utf-8" 在html标签中设置utf-8: meta charset="utf-8"

  4. Check character encoding in both your browser & text editor. 在浏览器和文本编辑器中检查字符编码。

  5. In MySQL, change the setting in my.cnf . 在MySQL中,更改my.cnf的设置。

I think the problem is my.cnf , and here is the tutorial about how to change it. 我认为问题是my.cnf ,这是关于如何更改它的教程。

Change MySQL default character set to UTF-8 in my.cnf? 在my.cnf中将MySQL默认字符集更改为UTF-8?

To set the default to UTF-8, you want to add the following to my.cnf 要将默认值设置为UTF-8,您需要将以下内容添加到my.cnf

[client]
default-character-set=utf8

[mysql]
default-character-set=utf8


[mysqld]
collation-server = utf8_unicode_ci
init-connect='SET NAMES utf8'
character-set-server = utf8

If you're using something like xampp, wamp, etc. 如果你正在使用像xampp,wamp等的东西。

Read this: How can I change my MySQL collation in WAMPSERVER 阅读: 如何在WAMPSERVER中更改我的MySQL排序规则

Find my.ini in X:\\wamp\\bin\\mysql\\mysql5.5.24\\my.ini 查找my.iniX:\\wamp\\bin\\mysql\\mysql5.5.24\\my.ini

(what ever you use, it will be in your MySQL directory.) (无论你使用什么,它都将在你的MySQL目录中。)

Updated at 2018/NOV/06 更新于2018年/ NOV / 06

In PHP7, mysql has been removed. 在PHP7中,mysql已被删除。 Like @Rick James said, for db connection, better use mysqli or PDO. 就像@Rick James所说,对于db连接,最好使用mysqli或PDO。

PHP manual - mysql_connect PHP手册 - mysql_connect

Warning - This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. 警告 - 此扩展在PHP 5.5.0中已弃用,并已在PHP 7.0.0中删除。

mysqli or PDO - what are the pros and cons? mysqli或PDO - 有什么优缺点?

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

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