PHP+MYSQL encoding, utf8_polish_ci

Hello i have problem with encoding in my script. My connect function looks like:

function connect()
{
    $conn = mysql_connect('192.168.1.127', 'mason_frik', 'difficultpassword');
    if (!$conn) 
    {
        die('Nie można się połaczyć!');
    }
    mysql_query("SET NAMES 'utf8'; COLLATE='utf8_polish_ci';");
    mysql_query("SET character_set_client = 'utf8'");
    mysql_query("SET character_set_results = 'utf8'");
    mysql_query("SET character_set_connection = 'utf8'");
    mysql_select_db('mason_konkursy');
}

In my database i'm using utf8_polish_ci everywhere. In my script i'm getting something from other page and i need to search it in my db like this: //this function is parsing other page and get innertext of SPAN.

$question = GetSpanData($FirstQuestion, "dnn_ctr1975_ViewContestsContestNew_dc_question_lblQuestion");



$wyn = mysql_query('SELECT * FROM questions WHERE question="'.$question.'"'); 
$wynik = mysql_fetch_array($wyn, MYSQL_ASSOC);

Result is bool(false). When i copy query to my phpmyadmin and paste to sql it is working, but from my script it didn't. Can You help me?

Answers


This line is wrong:

mysql_query("SET NAMES 'utf8'; COLLATE='utf8_polish_ci';");

The correct syntax is:

SET NAMES 'charset_name' COLLATE 'collation_name'

In your case, the code would the be:

mysql_query("SET NAMES 'utf8' COLLATE 'utf8_polish_ci';");

And read the first comment posted by eggyal: mysql_* functions should not be used anymore, they are deprecated and will be removed from PHP in a future version.

You may be missing a call to mysql_real_escape_string in this line of code (it is not possible for me to be sure, it depends what the function GetSpanData exactly does):

mysql_query('SELECT * FROM questions WHERE question="'.$question.'"');

to escape the data properly, you must use the mysql_real_escape_string function:

mysql_query('SELECT * FROM questions WHERE question="'.mysql_real_escape_string($question).'"');

Need Your Help

C preprocessor __TIMESTAMP__ in ISO 8601:2004

c gcc clang c-preprocessor icc

How can I have a __TIMESTAMP__ replacement in ISO 8601:2004?