Here … I am learning PHP. I got to the connection with the database.
And here’s the problem.
In the * .php file I register the connection to the database.
Then I write the queries, display the results on the screen.
Everything works. Does not give errors. BUT! Problem:
Russian text is not recognized. Displays signs ???
In this case, English letters are normally displayed.
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’m writing using the old PHP version, and for PHP 5.x
this method is not suitable. But I study from a book, there it is
written …
On the web found a solution to my problem.
But I’ve been reading for an hour already, my eyes are already red,
I can’t figure out what I’m doing wrong. If you write based on
examples that are written there, then I have three errors at once
issues. In general, how to use it correctly?
Nothing helps. What should be done?
Please tell me.
The code that I wrote from the book is like this:
& lt;? 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"];
? & gt;
Only a big request to everyone. Let’s not talk here about why a girl needs programming. We have already talked about this issue. I study – it means it is necessary. Thanks for your understanding 🙂
Answer 1, authority 100%
// Connect
mysql_connect ("localhost", "user", "pass");
mysql_select_db ("db");
mysql_set_charset ("utf8")
or if using mysqli
$ mysqli = new mysqli ("localhost", "user", "pass", "bd");
$ mysqli- & gt; set_charset ("utf8")
// Further work with the base
When creating a database, also use utf8_general_ci encoding, or translate the current one into it. Also set the header charset = UTF-8 and translate the encoding of the file itself (where you write the code and all files in general) into UTF-8 encoding.
After understanding the syntax, I advise you to do everything in mysqli (rather than mysql). it is more convenient, there is support, well, OOP is natural, but you will find out later) Good luck.
Answer 2, authority 33%
Make it a rule to write in UTF-8
.
- Save your scripts as encoded
utf-8
- Serve headers that you script
generates content inutf-8
- After a successful connection to the database, immediately execute the following query:
'SET NAMES utf8';
I use the principles listed above, and there is no problem with the encoding.
Answer 3, authority 11%
Well, here’s a “new” code written:
& lt;! DOCTYPE html PUBLIC "- // W3C // DTD XHTML 1.0 Transitional // EN" "http: // www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
& lt; html xmlns = "http://www.w3.org/1999/xhtml" & gt;
& lt; head & gt;
& lt; meta http-equiv = "Content-Type" content = "text / html; charset = windows-1251" / & gt;
& lt; title & gt; MySQL Database Connection & lt; / title & gt;
& lt; / head & gt;
& lt; body & gt;
& lt;? php
$ link = mysqli_connect ('localhost', 'one', '12345', 'firstbd');
if (mysqli_connect_errno ()) {
echo ("Failed to connect:% s \ n", mysqli_connect_error ());
exit ();
}
if (! mysqli_set_charset ($ link, "utf8")) {
echo ("Error loading utf8 character set:% s \ n", mysqli_error ($ link));
} else {
echo ("Current character set:% s \ n", mysqli_character_set_name ($ link));
}
if ($ result = mysqli_query ($ link, "SELECT * FROM firma")) {
echo ("& lt; br & gt; Select returned% d rows. \ n", mysqli_num_rows ($ result));
$ myrow = $ result- & gt; fetch_array (MYSQLI_ASSOC);
echo "& lt; br & gt;". $ myrow ['name'];
echo "& lt; br & gt;". $ myrow ['surname'];
mysqli_free_result ($ result);
}
mysqli_close ($ link);
? & gt;
& lt; / body & gt;
& lt; / html & gt;
Now no error leaves.
The screen shows what is obtained as a result.
Also, at the very beginning in Meta, I have written Windows-1251. If I change on UTF-8, then everything comes out by krakoyabram. How can I change?