This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 9 years ago.
I have this part of code that works fine only the arabic issue.
$result = mysql_query("SELECT login, password, name, role FROM qm_users WHERE login = '$login'");
if (!$result) {
die('Invalid query: ' . mysql_error());
}
$row = mysql_fetch_array($result);
echo $row["name"];
The name row contains arabic name.
The output of echo is ???? ???? ????
In my myphpadmin of xamp server, here the structure
Check below possible solution,
1) Your table structure 'Collation' must be 'utf8_unicode_ci'.
2) Also try to set below thing in php file
ini_set('default_charset','utf-8');
mysql_set_charset('utf8');
header('Content-type: text/html; charset=utf-8');
If you are going to save UTF8 encodings to your MySQL database first set your connection encoding to UTF8.
<?php
$connection = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
mysqli_set_charset($connection, "utf8");
?>
Related
This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 6 years ago.
Hey so i'm getting text with latin characters like "ó" from a mysql database. This is the connection along with the query i'm using to get the data, placing it in an array:
<?php
header('Content-Type: text/html; charset=UTF-8');
$con=new mysqli ("localhost","root","","chroniclemark");
$sqlget = "SELECT descricao FROM items;";
$sqldata = mysqli_query($con,$sqlget) or die ('error getting database');
while ($row = mysqli_fetch_array($sqldata, MYSQLI_ASSOC)) {
$lol5[] = $row['descricao'] ;
}?>
I'm then trying to display the the text outside of the <?php ?> statement like so:
<p ><?php echo $lol5[0]; ?></p>
All of the text is fine except for the "ç", "ó", "ã" and so on.
I also have a meta tag <meta charset="UTF-8"> in my header but it doen't help.
In the database, the data is set to "utf8mb4_unicode_ci".
What can i do to fix this? Any idea?
#Fred -ii Thanks for pointing me in the right direction in the comments.
All i had to do was use mysqli_set_charset($con, 'utf8mb4'); before making the query
$sqlget = "SELECT descricao FROM items;";
$sqldata = mysqli_query($con,$sqlget) or die ('error getting database');
It's all fine now :)
This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 1 year ago.
I have a select statement which should select all data in the database which equals to a Arabic input, I've set database's collation to utf8_general_ci and tables column as them for sure. And at the file
$conn = mysqli_connect($mysql_host, $mysql_user, $mysql_password, $mysql_database);
mysqli_query($conn, "set names 'utf8'");
mysqli_query($conn, "SET character_set_results=utf8");
mb_http_output('UTF-8');
mb_internal_encoding('UTF-8');
mb_http_input('UTF-8');
mb_language('uni');
mb_regex_encoding('UTF-8');
ob_start('mb_output_handler');
mb_language('uni');
mb_internal_encoding('UTF-8');
mysqli_set_charset($conn, "utf8");
And the code of the search
$question = $_REQUEST['question'];
$table = $_REQUEST['subject'];
if(empty($question)) {
echo "Type a question!"; }
$qe = mysqli_escape_string($conn, $question);
$full = "SELECT * FROM $table WHERE question LIKE '%$qe%'";
$fullQ = mysqli_query($conn, $full);
if(!$fullQ) {
echo mysqli_error($conn);
}
echo "<meta http-equiv='Content-Type' content='text/html;charset=UTF-8'>";
printf(nl2br("\n%u\n"), mysqli_num_rows($fullQ));
while($row = mysqli_fetch_assoc($fullQ)) {
printf(nl2br("\n%s\n"), $row['question']);
}
It works if I print all the data without using WHERE and it also prints the SQL syntax after it is sent from the first page to the second one right with the correct Arabic characters. But it always return 0 rows.
If your database saved in arabic then set up connection string to the following
$con1 = mysqli_connect($HOSTURL,$DBUSER,$DBPASSWORD,$DBNAME);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
die('');
}
mysqli_query($con1,"SET NAMES utf8");
//else{echo "Connection Done";}
if your database is not encoded then you do not have to set utf8 in your connection string.
i have the mysql connection below:
$dbLink = mysql_connect('127.0.0.1', 'root', '');
mysql_set_charset('utf8', $dbLink);
mysql_select_db('test', $dbLink);
mysql_set_charset('utf-8');
it was working fine 1 week ago, but suddenly it is not showing the utf-8 or arabic or unicode contents which are stored in the database. but now even in the database the characters has been changed like سید اØمد Ø´Ûید Ú©ÛŒ صØÛŒØ and showing in the php the same. before everything was perfect. and the content was showing properly.
even now when i make a new entry in the database its showing fine but something went wrong with the old entries any help please.......
i have tried set names, mysql_set_charset & <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> but nothing happened. please help otherwise my site will be hanged up......
Use MYSQLi because MYSQL is now deprecated.
To set charset utf-8 use mysqli_set_charset() like this:
$dbLink = mysqli_connect('127.0.0.1', 'root', '');
mysqli_set_charset($dbLink , 'utf8');
mysqli_select_db($dbLink, 'test');
If for any reason it is still displaying wrong characters, add this to the beginning of your documents:
// Tell PHP that we're using UTF-8 strings until the end of the script
mb_internal_encoding('UTF-8');
// Tell PHP that we'll be outputting UTF-8 to the browser
mb_http_output('UTF-8');
UPDATE
Select some contents from your database and output the value using iconv() and see if it shows the wright charset:
<?php
$dbLink = mysqli_connect('127.0.0.1', 'root', '');
mysqli_set_charset($dbLink , 'utf8');
mysqli_select_db($dbLink, 'test');
function get_table_contents(){
global $dbLink;
$contents = array();
$query = "SELECT * FROM ||YOUR TABLE HERE||";
$result = mysqli_query($connection, $query);
while($content = mysqli_fetch_assoc($result)){
$contents[] = $content;
}
return $contents;
}
$data = get_table_contents();
?>
<html>
<head></head>
<body>
<?php echo iconv(mb_detect_encoding($data[0]['||YOUR COLUMN||'], "UTF-8,ISO-8859-1"), "UTF-8", $data[0]['||YOUR COLUMN||']); ?>
</body>
</html>
This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 8 years ago.
I'm trying to print values from the database, containing countrylist. One of the fields in this database is cyrilic. My PHP script looks like this:
include_once("db.php");
$query = mysql_query ("SELECT * FROM country");
$row = mysql_fetch_array($query);
do {
$id = strtolower($row["id"]);
$value = ($row["runame"]);
echo $id . " " . $value . '<br/>';
} while ($row = mysql_fetch_array ($query));
The output from this script looks like this:
ad ???????
ae ???????????? ???????? ???????
af ??????????
ag ??????? ? ???????
ai ????????
So, apparently I need to set UTF-8 for this PHP script... somehow... Does anyone know how to do this?
Thank you for your time!
Try using SET NAMES utf8 after connecting to MySQL:
mysql_query("SET NAMES utf8");
Or if you are using cp1251 in the database, set this encoding.
As the manual says:
SET NAMES indicates what character set the client will use to send SQL
statements to the server... It also specifies the character set that the server should
use for sending results back to the client.
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");