How do I set up the correct encoding for MySQL?
Here... I'm learning PHP. I reached the connection to the database. And here's the problem.
In the *.php file, I register a connection to the database. Then I write the queries and display the results on the screen.
Everything works. It does not issue errors. BUT! Problem: the Russian text is not recognized. Outputs the characters ??? While the English letters are normally output.
So I understand that there are problems with the encoding. I changed the encoding both in the php file itself and in the database. On windows-1251 and on utf-8 and utf8_general_ci.
Apparently I write on the old version of PHP, and for PHP 5. x this method is not suitable. But I'm learning from a book, it says so...
In the network found a solution to my problem.
But I've been reading for hours, my eyes are already red, I can't figure out what I'm doing wrong. If you write on the basis of the examples that are written there, then I immediately get three errors. In general, how to use it correctly? Nothing helps. What should I do? Please tell me.
Code, which I wrote from a book like this:
<?php
$db=mysql_connect("localhost","one","12345");
mysql_select_db("firstbd",$db);
$result=mysql_query("SELECT * FROM firma",$db);
$myrow=mysql_fetch_array($result);
echo $myrow["id_firma"];
echo " - ".$myrow["name"];
echo " ".$myrow["surname"];
echo " - ".$myrow["doljnost"];
?>
Just a big request to everyone. Let's not talk about why a girl needs programming here. We have already talked about this issue. I study - so it is necessary. Thanks for understanding:)
3 answers
// Подключение
mysql_connect("localhost","user","pass");
mysql_select_db("db");
mysql_set_charset("utf8")
Or if mysqli is used
$mysqli = new mysqli("localhost", "user", "pass", "bd");
$mysqli->set_charset("utf8")
// Дальше работа с базой
When creating a database, use the utf8_general_ci encoding, or translate the current one into it. Just put the charset=UTF-8 header and translate the encoding of the file itself (where you write the code and all files in general) to UTF-8 encoding.
After understanding the syntax, I advise you to do everything in mysqli (rather than mysql) because it is more convenient, there is support, and of course OOP, but you will find out later) Good luck.
Make it a rule to write in the UTF-8
encoding.
- Save your scripts in the encoding
utf-8
- Give away the headers that you want.
generates content in
utf-8
- After a successful connection to the database, run the following query immediately:
'SET NAMES utf8';
I use the principles listed above, and there is no problem with the encoding.
Well, here is the" new " written code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1251"/>
<title>Соединение с БД MySQL</title>
</head>
<body>
<?php
$link = mysqli_connect('localhost', 'one', '12345', 'firstbd');
if (mysqli_connect_errno()) {
echo("Не удалось подключиться: %s\n", mysqli_connect_error());
exit();
}
if (!mysqli_set_charset($link, "utf8")) {
echo("Ошибка при загрузке набора символов utf8: %s\n", mysqli_error($link));
} else {
echo("Текущий набор символов: %s\n", mysqli_character_set_name($link));
}
if ($result = mysqli_query($link, "SELECT * FROM firma")) {
echo("<br>Select вернул %d строк.\n", mysqli_num_rows($result));
$myrow = $result->fetch_array(MYSQLI_ASSOC);
echo "<br>".$myrow['name'];
echo "<br>".$myrow['surname'];
mysqli_free_result($result);
}
mysqli_close($link);
?>
</body>
</html>
There are no more errors coming out now.
The screenshot shows what the result is.
Also, at the very beginning, I have windows-1251 written in meta. If I change it to utf-8, then everything comes out in krakozyabry. How can this be changed?