The chat script displays entries from the database in a different encoding.

Enters in the desired cp1251, and displays scribbles. Everything is written in htaccess. The script itself:

<?php define("HOST", "localhost"); define("USER", "user"); define("PASSWORD", "Pass"); define("DB", "db"); /************************ FUNCTIONS /************************/ function connect($db, $user, $password){ $link = @mysql_connect($db, $user, $password); @mysql_query('set character_set_client="cp1251"'); // Это устанивки кодировки ввода, работают @mysql_query('set character_set_results="cp1251"'); @mysql_query('set collation_connection="cp1251_general_ci"'); mysql_query("SET NAMES 'cp1251'"); // Пробую это на вывод, в итоге каракули mysql_query("SET CHARACTER SET 'cp1251'"); if (!$link) die("Could not connect: ".mysql_error()); else{ $db = mysql_select_db(DB); if(!$db) die("Could not select database: ".mysql_error()); else return $link; } } // Здесь получение данных из базы function getContent($link, $num){ $res = @mysql_query("SELECT date, user, message FROM shoutbox ORDER BY date DESC LIMIT ".$num, $link); if(!$res) { die("Error: ".mysql_error()); } else { return $res; } } // Здесь вставка сообщения, работает норм. function insertMessage($user, $message){ $query = sprintf("INSERT INTO shoutbox(user, message) VALUES('%s', '%s');", mysql_real_escape_string(strip_tags($user)), mysql_real_escape_string(strip_tags($message))); $res = @mysql_query($query); if(!$res) die("Error: ".mysql_error()); else return $res; } /****************************** MANAGE REQUESTS /******************************/ if(!$_POST['action']){ header ("Location: index.php"); } else{ $link = connect(HOST, USER, PASSWORD); switch($_POST['action']){ case "update": $res = getContent($link, 10); while($row = mysql_fetch_array($res)){ $result .= "<p><strong>".$row['user']."</strong><img src=\"css/images/bullet.gif\" alt=\"-\" />".$row['message']." <span class=\"date\">".$row['date']."</span></p>"; } echo $result; break; case "insert": echo insertMessage($_POST['nick'], $_POST['message']); break; } mysql_close($link); } ?> 

    1 answer 1

    If the encoding of the site and the database does not match (some of the text on the site is displayed normally, and some of the text from the database is in the form of incomprehensible characters). It is necessary in the script that connects to the database to add commands that will indicate to MySQL server the encoding in which to display the text. Depending on which PHP library you use, the commands will look like this:

    For mysql library

     mysql_query("SET NAMES 'utf8' COLLATE 'utf8_general_ci'"); mysql_query("SET CHARACTER SET 'utf8'"); 

    For mysqli library

     mysqli_query($link, "SET NAMES 'utf8' COLLATE 'utf8_general_ci'"); mysqli_query($link, "SET CHARACTER SET 'utf8'"); 

    Where:

    • instead of utf8, you must specify the desired encoding (for example, cp1251)
    • instead of utf8_general_ci necessary encoding mapping (for example, cp1251_general_ci ). A complete list of MySQL encodings and comparisons is available in the documentation.
    • for the mysqli library, the first parameter is a pointer to a connection to the database - in your script it may be different from $ link, you can find out by looking at the name of the variable in the source code, which is assigned the result of the mysqli_connect () function.

    For the PDO library, when you initiate a new connection, you need to add the 4th parameter:

     $db = new PDO(DB_DSN, DB_USER, DB_PASS, array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''));