echoing arabic string from database like this? - php

I am fetching data with mysql_fetch_assoc() the string is Arabic it's echoing like this ?????
I tried encoding but not worked.
$dbc = mysql_connect("localhost", "root", "", "tedmon") or die("Error 3: " . mysql_error());
mysql_query("SET NAMES utf8");
mysql_query("SET character-set utf8");
$sqlForStudent = "SELECT `MONITORING_PROVINCE`,count(*) FROM `student_interview` group by `MONITORING_PROVINCE`";
$sqlForadmin = "SELECT `pro`,count(*) FROM `ttc_admin_interview` group by `pro`";
$sqlForteacherev = "SELECT `pro`,count(*) FROM `ttc_teachers_evaluation` group by `pro`";
$sqlForteacherint = "SELECT `pro`,count(*) FROM `ttc_teachers_interview` group by `pro`";
$studentset = mysql_query($sqlForStudent);
$adminset = mysql_query($sqlForadmin);
$teacherevset = mysql_query($sqlForteacherev);
$teacherintset = mysql_query($sqlForteacherint);
while($teacherevres = mysql_fetch_array($teacherevset)){
echo $teacherevres['pro'];
}

Change from
mysql_query("SET NAMES utf8");
mysql_query("SET character-set utf8");
To
mysql_query("SET NAMES 'utf8'");
mysql_query("SET CHARACTER SET utf8");
Make sure you have added charset in head your page.
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Note mysql extension is deprecated as of PHP 5.5.0, and has been removed as of PHP 7.0.0

Related

How to read and send arabic to database using mysqli

This code is for the mysql_, I want to change it to mysqli_.
Here is my code:
mysql_query("set character_set_server='utf8'");
mysql_query("set names 'utf8'");
The connection string is:
$con = mysqli_connect(host,user,password,database) or die('Unable to Connect');

Php mysql query charset

I have problem with query charset for mysql bd in php script.
I'm placing GET parameter inside select query and it work good for all Latin characters, but with Cyrillic characters it returns me empty table. If I place some value with Cyrillic in query instead of GET parameter, query works as I want. Likewise, I get result query inside php-Admin and it works.
All Cyrillic characters in result shows me correct.
I've checked everything: php-file is utf8, GET parameter value is utf8, mysql_client_encoding() returns me utf8.
I've tried all I found - nothing helps me.
header('Content-type:text/html; charset=utf-8');
mysql_set_charset('utf8', $link);
mysql_query("SET NAMES utf8", $link);
mysql_query("SET CHARACTER SET 'utf8'", $link);
mysql_query("SET SESSION collation_connection = 'utf8_general_ci'", $link);
mysql_query("SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'", $link);
All of this DOESN'T HELP.
Here is my php script.
<?php
$link = mysql_connect('localhost', 'r96036lg_searche', 'Jh1ZT4]^');
//mysql_set_charset('utf8', $link);
//mysql_query("SET NAMES utf8", $link);
//mysql_query("SET CHARACTER SET 'utf8'", $link);
//mysql_query("SET SESSION collation_connection = 'utf8_general_ci'", $link);
mysql_query("SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'", $link);
//echo mysql_client_encoding($link);
mysql_select_db('r96036lg_searche', $link);
//mysqli::set_charset('utf8');
$query = "select id, name, typeid from MainObjects where used = 1";
if(isset($_GET['name']) && !empty($_GET['name'])) {
//$name = mb_convert_encoding($_GET['name'], "UTF-8", "win1251");
$name = $_GET['name'];
//echo mb_detect_encoding($name, "auto", false);
//$name = iconv(mb_detect_encoding($name, "auto", false), "UTF-8", $name);
//echo $name;
$query .= " and typeid = 1 and (name like '%".$name.
"%' or id in (select parentid from MainObjects where name like '%"
.$name."%' and used = 1))";
}
//echo $query;
$result = mysql_query($query, $link);
if(!$result) echo mysql_error();
else {
$out = "[";
while($row = mysql_fetch_row($result)) {
... //here is myoutput
}
$out .= "]";
echo $out;
}
?>
This is how you should do it. It will not solve the caracter encoding thing, but it will help with the reste. (And perhaps even solve your problem, because it might just be related to not escaping the values).
$query = "select id, name, typeid from MainObjects where used = 1";
if(isset($_GET['name']) && !empty($_GET['name'])) {
$name = mysql_real_escape_string($_GET['name']);
$query .= " and typeid = 1 and (name like '%".$name.
"%' or id in (select parentid from MainObjects where name like '%"
.$name."%' and used = 1))";
}
//echo $query;
$result = mysql_query($query, $link);
I this does not help there is a character coding mismatch somewhere. There are three points:
GET data: did you try to encode the url like example.com/index.php?name=%d0%ba (cyrillic small letter ka)
communication with server. If you did really try all you said, there should not be an error
data in the tables: Does the collation of the table really correspond to its content. What I might suspect is that there is a mismatch. If the data is in UTF-8, but the table isn't, mysql will try to convert the data from one character set into another, and as a result it will not much.

Mysql insert encoding issue

I am trying to insert Utf-8 string in a cell with utf8_general_ci collation
My code:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
...
print mb_detect_encoding($name);
$query = "INSERT INTO items SET name='$name', type='$type'";
print $query;
mysql_set_charset('utf8'); // also doesn't help
$result0 = mysql_query("SET CHARACTER SET utf8") or die(mysql_error()); // I've added this line, but it doesn't solve the issue
$result01 = mysql_query('SET NAMES utf8') or die("set character ".mysql_error()); // still nothing
$result = mysql_query($query) or die(mysql_error());
What I see in the html output:
UTF-8 INSERT INTO waypoints SET name='ČAS', type='ts'
What I see in the database, as name in the inserted row:
?AS
Now my string is utf-8, my table and cell is utf-8 .. why the wrong encoding?
Ok guys, thank you all for help, it looks like I've solved the issue with:
mysql_query("SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'", $link);
right after creating the connection
(especially #Bartdude for the article, which gave me a hint)

change mysql table character set

I have a table that data was inserted in it some time ago. data included by php and character set in it was utf-8 .
mysql_query("set names utf8");
now ,I need this table in another project which all data will show on php mysql default character set.
problem : my data is persian, when I set charachter set utf-8, every things is ok, but without character set data convert to the "?????" what should I do?!
I want to import all data form old table to new table on new character set !!
Try this might help you
<?php
header('Content-Type: text/html; charset=utf-8');
?>
and then in the connection
<?php
$dbLink = mysql_connect($argHost, $argUsername, $argPassword);
mysql_query("SET character_set_results=utf8", $dbLink);
mb_language('uni');
mb_internal_encoding('UTF-8');
mysql_select_db($argDB, $dbLink);
mysql_query("set names 'utf8'",$dbLink);
?>
///
$SERVER = "http://localhost:8084";
$db = mysql_connect('localhost','root') or die("Not Connected");
$sldb = mysql_select_db('vifarbydata',$db);
mysql_query("set names utf8");
$sql = "SELECT * FROM `region` ";
$res = mysql_query($sql);
while($h=(mysql_fetch_array($res)))
{
$row[] = $h;
}
mysql_close($db);
/// set another character set
$SERVER = "http://localhost:8084";
$db = mysql_connect('localhost','root') or die("Not Connected");
$sldb = mysql_select_db('vifarbydata',$db);
foreach ($row as $item)
{
echo $sql = "insert into `region2` (id,name,myId) values (".$item['id'].",'".$item['name']."','".$item['myId']."') ";
mysql_query($sql);
}
#afsane, using mysqldump, you can dump the data from old_table and then import it into the new_table which has the new character encoding.
mysqldump -u user -p --no-create-info schema old_table > old_table.dump
mysql -u user -p schema new_table < old_table.dump
This will be much faster than doing a conversion via PHP.

Saving Japanese characters with PHP

I'm currently trying to save a japanese-character string with MySQL in PHP. The characters are saved as questionmarks. This is my query:
mysql_query("SET NAMES 'utf8'");
mysql_query("SET CHARACTER SET 'utf8'");
$result = mysql_query("INSERT INTO battles (basic_words) VALUES ('".mysql_real_escape_string($basic_words)."'");
The string "$basic_words" definitely contains japanese characters but they are not saved. The coalition for the row "basic_words" is utf8_general_ci
php mysql query encoding problem suggests
$db_con= mysql_connect('localhost', 'user', 'password');
if( function_exists('mysql_set_charset') ){
mysql_set_charset('utf8', $db_con);
}else{
mysql_query("SET NAMES 'utf8'", $db_con);
}
Also Check http://php.net/manual/en/function.mysql-set-charset.php

Categories