genesi5
genesi5

Reputation: 453

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:

<?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:

enter image description here

Any suggestions?

Upvotes: 3

Views: 202

Answers (1)

MAJA Lin
MAJA Lin

Reputation: 11

There are several ways to set character to utf-8.

  1. While creating the table with phpMyAdmin, select utf8_unicode_ci

  2. Add mysqli_query("SET NAMES 'UTF8′") when you're accessing the database.

  3. Set utf-8 in html tag: meta charset="utf-8"

  4. Check character encoding in both your browser & text editor.

  5. In MySQL, change the setting in my.cnf.

I think the problem is my.cnf, and here is the tutorial about how to change it.

Change MySQL default character set to UTF-8 in my.cnf?

To set the default to UTF-8, you want to add the following to 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.

Read this: How can I change my MySQL collation in WAMPSERVER

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

(what ever you use, it will be in your MySQL directory.)

Updated at 2018/NOV/06

In PHP7, mysql has been removed. Like @Rick James said, for db connection, better use mysqli or PDO.

PHP manual - mysql_connect

Warning - This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0.

mysqli or PDO - what are the pros and cons?

Upvotes: 1

Related Questions