3

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?

genesi5
  • 455
  • 3
  • 17
  • 1
    Possible duplicate of [UTF-8 all the way through](http://stackoverflow.com/questions/279170/utf-8-all-the-way-through) – Machavity Nov 29 '15 at 14:21
  • You need to [stop using mysql_ functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) as they are removed in PHP7 – Machavity Nov 29 '15 at 14:22
  • As @Machavity say do not use the mysql_functions. Try to set the collation for the utf type yo wish to use directly in the database. – Franco Nov 29 '15 at 14:26
  • You're very vulnerable to [SQL injection](https://www.owasp.org/index.php/SQL_Injection) – Bono Nov 29 '15 at 15:15
  • What text _should_ you be getting? (I need to work backward.) Please provide it in a way I can 'copy', not in a screen image. – Rick James Dec 11 '15 at 00:23
  • You may have to convert your code from `mysql_*` to `mysqli_*` to get this to work. – Rick James Dec 11 '15 at 00:25

1 Answers1

1

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?

MAJA Lin
  • 11
  • 1
  • 3