I have two forms on two different pages which are used to insert data to an MySQL database. I have some special character like 'čšžćđ' in my form data which I pass via the forms to the insertion scripts.
The data from the first form gets inserted correctly, while some fields from the second form contain the '?' characters, which would indicate a mismatch in encoding.
The two insertion scripts of both the forms are using the same file to connect to the database and set the encoding, like below:
<?php
$username = "root";
$password = "";
$servername = "localhost";
$conn = mysqli_connect($servername, $username, $password);
mysqli_select_db($conn, "testdb");
if (!$conn) { // check if connected
die("Connection failed: " . mysqli_connect_error());
exit();
}else{
/* change character set to utf8 */
if (!mysqli_set_charset($conn, "utf8")) {
// printf("Error loading character set utf8: %s\n", mysqli_error($conn));
} else {
// printf("Current character set: %s\n", mysqli_character_set_name($conn));
}
mysqli_select_db($conn, "testdb");
//echo "Connected successfully.";
// Check if the correct db is selected
if ($result = mysqli_query($conn, "SELECT DATABASE()")) {
$row = mysqli_fetch_row($result);
//printf("\nDefault database is %s.\n", $row[0]);
mysqli_free_result($result);
}
}
?>
I guess this would mean, that the client character encoding isn't set correctly? All database tables have the utf_8 encoding set.
Try to set encoding on top of the page
<?php
header('Content-Type: text/html; charset=utf-8');
other code...
Are you talking about HTML forms? If so,
<form accept-charset="UTF-8">
Is it one ? per accented character? When trying to use utf8/utf8mb4, if you see Question Marks (regular ones, not black diamonds),
The bytes to be stored are not encoded as utf8. Fix this.
The column in the database is CHARACTER SET utf8 (or utf8mb4). Fix this.
Also, check that the connection during reading is utf8.
The data was probably converted to ?, hence cannot be recovered from the text.
SELECT col, HEX(col) FROM ... to see what got stored.
? is 3F in hex.
Accented European letters will have two hex bytes per character. That includes each of čšžćđ.
Chinese, Japanese, and Korean will (mostly) have three hex bytes per character.
Four hex characters would indicate "double encoding".
Related
I'm struggeling with a tiny script which is responsible for 2 things:
- truncating database
- uploading files into database
Looks like that:
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
$mysql_host = 'localhost';
$mysql_username = 'x';
$mysql_password = 'y';
$mysql_database = 'z';
$db = new PDO('mysql:dbname='.$mysql_database.';host='.$mysql_host,$mysql_username,$mysql_password);
// works not with the following set to 0. You can comment this line as 1 is default
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, 1);
function truncate_db()
{
global $db;
$sql_query_1 = "
TRUNCATE TABLE `VISITS`;
TRUNCATE TABLE `ANIMALS`;
TRUNCATE TABLE `DOCTORS`;
TRUNCATE TABLE `PAYMENTS`;
TRUNCATE TABLE `CUSTOMER`
";
try {
$stmt = $db->prepare($sql_query_1);
$stmt->execute();
echo "Truncate action - OK";
}
catch (PDOException $e)
{
echo $e->getMessage();
die();
}
}
function import_db()
{
global $db;
try
{
$sql_query_2 = implode(array_map(function ($v) {
return file_get_contents($v);
}, glob(__DIR__ . "/*.sql")));
$qr = $db->exec($sql_query_2);
echo "Import action - OK";
}
catch (PDOException $e)
{
echo 'Connection failed: ' . $e->getMessage();
}
}
truncate_db();
echo '<br />';
import_db();
$db = null;
?>
Issue - files (sql one) which I'm uploading to the database contains special charaters (like ś, ó, ę etc.) After that I have an issue in the database that some of the words doesn't contain any more those symbols. After upload I have symbols like: ³, ¿ etc. How can I edit function import_db() to keep those characters? i thought about:
mb_convert_encoding
but I have no clue how to incorporate that into my code ;/ in my DB table, column with that words (containing special characters) is set to: UTF8_General_CI. thanks!
I'm unclear on what your problem is
Truncated text is when you try to insert Señor, but find that only Se shows up in the table. This form of truncation is usually caused by
The client had accented characters encoded in latin1 (or latin2, etc), and
SET NAMES utf8 was in effect during the INSERT (or LOAD DATA)
That is, you should either get the text in utf8 or you should change what you tell MySQL the encoding is.
If you can get the hex of the file contents, ś ó ę should be 2-byte hex C59B C3B3 C499 in utf8. latin1 only has ó as a single byte F3. In latin2, those are B6 F3 EA, so perhaps that is where you are coming from?
It is OK to have a mismatch between what SET NAMES says and what CHARACTER SET you have established to table/column. MySQL will convert the encoding as they are transferred.
Do not use mb_convert_encoding or any other conversion functions when using MySQL, it only adds to the confusion.
Also, do the TRUNCATEs one at a time. The API does not like multiple statements.
I have some text in a database. I use French and English. French has accents, and some special characters like ç. I use Mamp, MySQL and PHP.
I have collation latin1_swedish-ci (the default). I tried utf8_general_ci and the result is the same.
If I use in a html page, I have this in the head: <meta charset="UTF-8">
As an example, in the database I have "voilà".
When I echo the text from the database to html:
$con = mysqli_connect("localhost","root","root");
if (!$con) {
die('The connexion failed: ' . mysqli_error());
}
if (!mysqli_select_db($con, 'prova')){
echo "Connection with database was not possible";
}
$result = mysqli_query($con, "SELECT * FROM test1
WHERE id='1' ")
or die(mysqli_error());
while($row = mysqli_fetch_array($result)) {
$text = $row['first'];
echo $text; //I see: voil�
echo htmlentities($text); //I see nothing
echo utf8_encode($text); //This works: I see voilà
}
Why htmlentities does not work?
Is utf8_encode(); the way to go? I have to use that always when I output something from the database? Why do I have to use that if the collation is already UTF8? Is there any better way to store and output text with accents in a MySQL database?
After you connect to the DB you should set the client charset to UTF8:
mysqli_set_charset($con, "UTF8");
Otherwise the mysql client transforms the UTF8 'voilà' to latin1 ('cause it seems that is it's default).
Either you tell the client that I want everything in UTF8, or you get it with the default latin1, and convert it one-by-one yourself calling utf8_encose($text)
I have a problem, when I try to echo a cyrillic character, it return like ????
Here's code
<?
include('db.php');
$sql = "SELECT * FROM menu_items WHERE reference=1";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$rows = array();
while($row = $result->fetch_object()) {
$rows[] = json_encode($row);
}
$items = implode(',',$rows);
echo '['.$items.']';
}else {
echo "ERROR";
}
?>
Any idea?
Collation : utf8_general_ci
And db.php:
<?
$servername = "localhost";
$username = "test";
$password = "Conqwe333!";
$conn=mysqli_connect($servername,$username,$password,"test");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
Worked after <? $conn->set_charset("utf8");?>
Add before your $sql
$conn->query('SET NAMES utf8');
You can read more about it here
Also you will need to set proper header for browser. You can do it by serveral ways for example in meta html tag or using header('Content-Type: text/html; charset=utf-8');
You should set collation per connection:
mysqli_set_charset
Also you can perform sql
SET NAMES utf8;
but it's not recommended
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "test");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
/* change character set to utf8 */
if (!$mysqli->set_charset("utf8")) {
printf("Error loading character set utf8: %s\n", $mysqli->error);
} else {
printf("Current character set: %s\n", $mysqli->character_set_name());
}
$mysqli->close();
I am assuming you are using Bulgarian and UTF8, same will work for Russian and other languages, just change "bg" to proper string.
I do not recommend you to use cp1251, because it breaks unexpectedly with apache mod_rewrite and other tools like this.
You need to do following checks:
Check if your database / table collation is some UTF8. It could be utf8_general_ci or Bulgarian - difference is minimal and is more sorting related. (utf8_general_ci is perfectly OK)
Check you have following statement executed right after connect - set names UTF8;. You can do $mysqli->query("set names utf8");
Make sure you have proper "tags". Here an example:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang='bg' xml:lang='bg' xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Нов сайт :)</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf8">
You can include UTF8 "BOM" on the html, but it works pretty well without it. I usually work without "BOM", and when I want to be 100% complaint, I create an include file bom.php that contain just the BOM symbol and include it prior HTML template in normal PHP way, e.g. include "bom.php".
Hope this helps, if not, please comment.
EDIT:
Someone suggested you must be sure if your data is properly stored in MySQL. Easiest way is to open PHP MySQL Admin. If Cyrillic is shown there, all is OK.
I think the issue is a step back, try to first encode the cyrillic characters correctly: How to encode cyrillic in mysql?
I am inserting data in mysql table , prior to that i am escape sequencing variable
$data = mysqli_real_escape_string($con,$data);
Every thing is working fine , but when i am getting data like this
$data = " SHR′ n(X′) ";
its escaping data like this
SHR′ n(X′)
which is not inserting in database and giveing error, So my question is how can i make it to escape this kind of characters.
Note:I have created a table with utf8_general_ci as collation.
Thanks
Try this
after establish connection use
mysqli_set_charset ($con, "utf8");
Try to set the charset to utf8.
$mysqli = new mysqli("localhost", "my_user", "my_password", "test");
/* change character set to utf8 */
if (!$mysqli->set_charset("utf8")) {
printf("Error loading character set utf8: %s\n", $mysqli->error);
} else {
printf("Current character set: %s\n", $mysqli->character_set_name());
}
This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 12 months ago.
I have a mysql table with contents
the structure is here:
I want to read and print the content of this table to html
This is my code:
<?php
include("config.php");
$global_dbh = mysql_connect($hostname, $username, $password)
or die("Could not connect to database");
mysql_select_db($db)
or die("Could not select database");
function display_db_query($query_string, $connection, $header_bool, $table_params) {
// perform the database query
$result_id = mysql_query($query_string, $connection)
or die("display_db_query:" . mysql_error());
// find out the number of columns in result
$column_count = mysql_num_fields($result_id)
or die("display_db_query:" . mysql_error());
// Here the table attributes from the $table_params variable are added
print("<TABLE $table_params >\n");
// optionally print a bold header at top of table
if($header_bool) {
print("<TR>");
for($column_num = 0; $column_num < $column_count; $column_num++) {
$field_name = mysql_field_name($result_id, $column_num);
print("<TH>$field_name</TH>");
}
print("</TR>\n");
}
// print the body of the table
while($row = mysql_fetch_row($result_id)) {
print("<TR ALIGN=LEFT VALIGN=TOP>");
for($column_num = 0; $column_num < $column_count; $column_num++) {
print("<TD>$row[$column_num]</TD>\n");
}
print("</TR>\n");
}
print("</TABLE>\n");
}
function display_db_table($tablename, $connection, $header_bool, $table_params) {
$query_string = "SELECT * FROM $tablename";
display_db_query($query_string, $connection,
$header_bool, $table_params);
}
?>
<HTML><HEAD><TITLE>Displaying a MySQL table</TITLE></HEAD>
<BODY>
<TABLE><TR><TD>
<?php
//In this example the table name to be displayed is static, but it could be taken from a form
$table = "submits";
display_db_table($table, $global_dbh,
TRUE, "border='2'");
?>
</TD></TR></TABLE></BODY></HTML>
but I get ???????? as the results:
Where is my mistake?
Four good steps to always get correctly encoded UTF-8 text:
1) Run this query before any other query:
mysql_query("set names 'utf8'");
2) Add this to your HTML head:
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
3) Add this at top of your PHP code:
header("Content-Type: text/html;charset=UTF-8");
4) Save your file with UTF-8 without BOM encoding using Notepad++ or any other good text-editor / IDE.
Set the charset as utf8 as follows:
$conn = new mysqli($servername, $username, $password, $dbname);
$conn->set_charset("utf8");
You are not defining your HTML page as UTF-8. See this question on ways to do that.
You may also need to set your database connection explicitly to UTF8. Doing a
mysql_query("SET NAMES utf8;");
^
Put it right under your database connection script or include and MAKE sure you have it placed before you do any necessary queries. Also, for collocation please take the time to make sure your
setting it for your proper syntax type and general_ci seems working good for me when used. As a finale, clear your cache after banging your head, set your browser to proper encoding toolbar->view->encoding
Setting the connection to UTF8 after establishing the connection takes care of the problem. Don't do this if the first step already works.
UTF-8 content from MySQL table with PDO
To correctly get latin characters and so on from a MySQL table with PDO,
there is an hidden info coming from a "User Contributed Note" in the PHP manual website
(the crazy thing is that originally, that contribution was downvoted, now luckily turned to positive .. sometime some people need to got blamed)
my credits credits go to this article that pulled the solution and probably made that "User Contributed Note" to turn positive
If you want to have a clean database connection with correct Unicode characters
$this->dbh = new PDO(
"mysql:host=".DB_HOST.";dbname=".DB_NAME.";charset=utf8",
DB_USER,
DB_PASS);
try this :
mysql_set_charset('utf8', $yourConnection);
Old ways have been deprecated. If you are using PHP > 5.0.5 and using mysqli the new syntax is now:
$connection->set_charset("utf8")
Where $connection is a reference to your connection to the DB.
I tried several solutions but the only one that worked
is that of Hari Dass:
$conn->set_charset("utf8");