S
S
Sebastian Pereira2014-01-30 16:15:26
PHP
Sebastian Pereira, 2014-01-30 16:15:26

How to pass query from php to mysql, encoding 1252?

Hello. Faced a problem.
The data in my database uses encoding 1252 (mysql)
From php, I need to send a request to the database, for which I will recode my entry to 1252 like this:

$string = "Привет";
$string = iconv('utf-8', 'cp1251', $string);
$string = iconv('cp1252', 'utf-8', $string);

Everything is ok, now I am accessing the database with a request:
$textsql='SELECT id FROM inv_devices where staff="'.$string.'"';
$answsql=mysql_query($textsql);
$id1=mysql_result($answsql,0,0);

and see
Warning: mysql_result() [function.mysql-result]: Unable to jump to row 0 on MySQL...

If I connect directly to the database and do
SELECT id FROM inv_devices where staff="<b>Ïðèâåò</b>"

That's all okay. Slect works. Please tell me how to solve my problem.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Andrew, 2014-01-30
@kaasius

So far, all that is visible is some kind of crookedness.

$string = iconv('utf-8', 'cp1251', $string);
$string = iconv('cp1252', 'utf-8', $string);

That is, you convert from utf8 to 1251 , and then convert this resulting string from 1252 to utf8? It looks a lot like bullshit.
If anything, all modern databases keep all data in utf8 inside. Therefore, if the site works for you in utf8, then you just need to correctly set the encoding for connecting to the database, and do not brainwash yourself and your colleagues :)

P
Push Pull, 2014-01-30
@deadbyelpy

Maybe it will help mysql_query("set names 'utf8'");
Although it's true, it's not clear why to drive encodings back and forth.

T
Terminaft, 2014-02-03
@Terminaft

Can you try this

mysql_query("SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'", $db);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question