I have a PHP script that gets a XML, selects some data, and insert them in my database (MySQL). All my fields are utf8_bin. This XML is ISO-8859-1, and can't change this because is another site that sends it to me.
Example: the string "NG Contábil" is set "NG Contábil" in my db. This is my script:
<?php
header('Content-Type: text/html; charset=utf-8');
mysql_query("SET NAMES 'utf8'");
mysql_query('SET character_set_connection=utf8');
mysql_query('SET character_set_client=utf8');
mysql_query('SET character_set_results=utf8');
include '/PagSeguroLibrary/PagSeguroLibrary.php';
include '/PagSeguroLibrary/domain/PagSeguroAccountCredentials.class.php';
include 'conexao.php';
include 'alias_array.php';
include 'retorna_cpf.php';
$conexao = ConectaBD::get_instance();
$conexao->conectar_pronto();
$conexao->BD_pronto();
//(...)
$xml = simplexml_load_file('arquivo.xml');
if($xml === null)
$xml = simplexml_load_string($resposta_transacao);
$status = $xml->status;
$nome = $xml->sender->name;
$email = $xml->sender->email;
$codigo = $xml->code;
$vetor = $xml->items->item;
$cpf = retorna_cpf($email);
foreach($vetor as $v)
{
$nome_produto = $v->description;
if($nome_produto != 'frete')
{
//Retorna o id do produto a partir da descrição
$result = mysql_query('SELECT id_product
FROM ps_product_lang
WHERE name = "'.$nome_produto.'"');
$array = Array();
while($row = mysql_fetch_alias_array($result))
{
foreach($row as $linha)
array_push($array, $linha);
}
mysql_query('INSERT INTO pagamento(Status, Nome, Email, CPF, idproduto, Codigo, Inscrito, id, Enviado, NomeProduto)
VALUES ("'.$status.'", "'.$nome.'", "'.$email.'", "'.$cpf.'", "'.$array[0].'", "'.$codigo.'", 0, null, 0, "'.$nome_produto.'")');
}
}
fclose($arquivo);
unlink('arquivo.xml');
?>
Thanks for any answer!
You have overlooked just one little nuance:
mysql_query("SET NAMES 'utf8'"); is not a magical spell that have to be cast in order to make proper encoding, but actually an SQL query, which needs to be run in the same instance you are actually using to run SQL queries.
So, if you are connecting to your mysql database using ConectaBD::get_instance(); you have to run SET NAMES utf8 query after that call, not before.
I don't know why, but adding utf8_decode() solves the problem
I know.
simplexml's output is always utf-8.
While, as I pointed out above, you don't set your client connection into utf8.
So, it remains default latin1
With (quite useless) utf8_decode() call you're casting your utf-8 data back into latin1 and thus it correctly stored into database.
This should solve your issue:
...
$xml = simplexml_load_file('arquivo.xml');
$xml = iconv('ISO-8859-1', 'UTF-8', $xml);
...
Related
How can i get arabic data from MSSQL database using ODBC connection?
It appears as ????.
I tried below listed solutions which are not working for me.
1) 'Name'=>iconv("unicode", "utf-8", $row["Name"]),
2) 'Name'=>iconv("Arabic_CI_AS", "utf-8", $row["Name"]),
3) mysql_query("SET NAMES utf8");
4) mysql_query('SET CHARACTER SET utf8');
5) <meta charset="UTF-8" />
6) 'Name'=>mb_convert_encoding($row["Name"], 'UTF-8', 'SJIS'),
Code snippet:
$customer_query="select serial,CR_ID,Name,Email,Customer_NameE from [CRM].[005_Customers_tbl]";
$query = odbc_exec($link,$customer_query);
$total_count=odbc_num_rows($query);
$json = array();
if($total_count>0){
while ($row = odbc_fetch_array($query)) {
$json[] = array('serial'=>$row['serial'],
'CR_ID'=>$row['CR_ID'],
'Name'=>$row["Name"],
'Email'=>$row['Email'],
'Customer_NameE'=>$row['Customer_NameE']);
}
}
$result['customer']=$json;
I know that it's achievable using change collation of table but it's not possible for this application due to some limitations.
Any other possibilities to achieve that ?
First you have to put below line
<meta charset="UCS-2"/>
and do casting in query like this
$var = "select CAST(Name as VARBINARY(150) as Name) form $table_name;
and then use this line for convert ??? text to arabic language.
$name = iconv('UCS-2LE','UTF-8',$name)
Problem
I am getting 0 results when searching for an Arabic word in a MySQL database using the SELECT query with PHP.
However, the same exact query yields results in alternative clients, namely SQLBuddy and the likes. Everything is encoded in UTF-8.
Code
<?php
$host = "localhost";
$username = "hans_wehr_client";
// i know my security is a joke :)
$password = "hans_wehr";
$database = "hans_wehr";
$conn = new mysqli($host, $username, $password, $database);
if ($conn == TRUE){
$search = $_GET["search"];
$encoded_search = utf8_encode($search);
echo $encoded_search."<br>";
header('Content-Type: text/html; charset=utf-8');
$sql = "SELECT * FROM dictionary WHERE ARABIC LIKE '$search'";
echo $sql."<br>";
mysqli_query($conn,"SET NAMES 'utf8'");
mysqli_query($conn,'SET CHARACTER SET utf8');
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
header('Content-Type: text/html; charset=utf-8');
echo $row["ARABIC"]. " - Meaning: " . $row["ENGLISH1"]. " " . $row["ENGLISH2"]. "<br>";
}
}else {
echo "0 results";
}
}
?>
Before the mods get the pitchforks, I have to clear up my troubleshooting logic.
Encoding. I set the page encoding to utf-8 using header('Content-Type:
text/html; charset=utf-8'); and ran the queries mysqli_query($conn,"SET NAMES 'utf8'"); and mysqli_query($conn,'SET CHARACTER SET utf8');, this cleared up the ??????? and Ùؤتا
rendered instead of Arabic words issue. That is kind of a different
issue. Source. and Source2.
Database Charset. My database and columns are set to UTF-8.
Other clients work. SQLBuddy/MySQL native client/ PHPMyAdmin appear to be working because running the same exact query yields result. Therefore I appear to be on the same bloody boat with him. The query SELECT * FROM dictionary WHERE ARABIC LIKE 'آخَر، أُخرى' returns a result on SQLbuddy but nada on PHP.
Possible solution:
Running the query SELECT * FROM dictionary WHERE ARABIC LIKE 'آخَر، أُخرى' yields me a result.
However running the query with a UTF-8 encoded version of the Arabic word returns 0 results. SELECT * FROM dictionary WHERE ARABIC LIKE 'آخَر، أُخرى' I think this simulates PHP.
The UTF-8 Arabic word version is obtained by decoding the automatically URL encoded $[_GET] parameter i.e %26%231570%3B%26%231582%3B%26%231614%3B%26%231585%3B%26%231548%3B+%26%231571%3B%26%231615%3B%26%231582%3B%26%231585%3B%26%231609%3B
Could it be that the MySQLi actually queries the UTF-8 version instead of the actual Arabic word? Therefore finding no match since they are different?
If so how can I explicitly tell PHP not to URL encode my search term and therefore pass it as it is?
Since according to my tinfoil theory, http://localhost/hans_wehr/search_ar.php?search=آخَر، أُخرى would work but http://localhost/hans_wehr/search_ar.php?search=%26%231570%3B%26%231582%3B%26%231614%3B%26%231585%3B%26%231548%3B+%26%231571%3B%26%231615%3B%26%231582%3B%26%231585%3B%26%231609%3B
Inputs will be greatly appreciated.
Use html_entity_decode():
Use html_entity_decode() on your $_GET["search"] value
<?php
$host = "localhost";
$username = "hans_wehr_client";
// i know my security is a joke :)
$password = "hans_wehr";
$database = "hans_wehr";
$conn = new mysqli($host, $username, $password, $database);
if ($conn == TRUE){
$search = $_GET["search"];
$encoded_search =html_entity_decode($search, ENT_COMPAT, 'UTF-8');
echo $encoded_search."<br>";
header('Content-Type: text/html; charset=utf-8');
$sql = "SELECT * FROM dictionary WHERE ARABIC LIKE '$encoded_search'";
echo $sql."<br>";
mysqli_query($conn,"SET NAMES 'utf8'");
mysqli_query($conn,'SET CHARACTER SET utf8');
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
header('Content-Type: text/html; charset=utf-8');
echo $row["ARABIC"]. " - Meaning: " . $row["ENGLISH1"]. " " . $row["ENGLISH2"]. "<br>";
}
}else {
echo "0 results";
}
}
?>
I am trying to fetch "hindi" (utf-8) results from MySQL database using php.
I have written the following code..
<?php
$connection = mysql_connect("localhost","root","root");
if(!connection)
{
die('Could not connect: ' . mysql_error());
}
mysql_query('SET character_set_results=utf8');
mysql_query('SET name=utf8');
mysql_query('SET character_set_client=utf8');
mysql_query('SET character_set_connection=utf8');
mysql_query('SET character_set_results=utf8');
mysql_query('SET collation_connection=utf8_general_ci');
mysql_select_db('data',$connection);
$result = mysql_query("SET NAMES utf8");
$cmd = "select * from question";
$result = mysql_query($cmd);
$myArray = array();
while($row =$result->fetch_object()){
$tempArray = $row;
array_push($myArray, $tempArray);
}
echo json_encode($myArray);
?>
But I am not getting the expected results. I want to print Utf-8(Hindi) data.
if I use the following code to print individual row then
while($myrow = mysql_fetch_row($result)){
echo ($myrow[0]);
echo ($myrow[1]);
echo ($myrow[2]);
}
It works fine and prints the utf-8(Hindi) data.
Please help me what I am missing or is there anything wrong implemented....
Thanks in advance..!!
mysql_query('SET name=utf8');
Should be mysql_query('SET NAMES utf8');. The other SETs are not needed.
Also, don't use the deprecated mysql_* interface, rewrite to use mysqli_* (or PDO).
By Hindi, do you mean DEVANAGARI, such as हिन्दी ?
The utf8 hex for that is: E0A4B9 E0A4BF E0A4A8 E0A58D E0A4A6 E0A580
If you see something like हिनà¥à¤¦à¥€, you have "Mojibake".
not getting the expected results
What are you getting?
json_encode -- should include a second argument of JSON_UNESCAPED_UNICODE to avoid getting the "html entities".
To see hex in PHP,
$hex = unpack('H*', $text);
echo implode('', $hex);
latin1 will not suffice; you need utf8 (or utf8mb4).
I realize this is not a full or coherent answer, but it presents several action items for the OP to get closer to the solution.
I have a database UTF8 encoded, because it contains arabic letters. I have a php file to select all values from a given table:
<?php header('Content-type: application/json; charset=utf-8');
$host = "localhost"; //Your database host server
$db = "kalimat"; //Your database name
$user = "root"; //Your database user
$pass = ""; //Your password
$connection = mysqli_connect($host, $user, $pass);
//Check to see if we can connect to the server
if(!$connection)
{
die("Database server connection failed.");
die(mysqli_error($db));
}
else
{
//Attempt to select the database
$dbconnect = mysqli_select_db($connection, $db);
//Check to see if we could select the database
if(!$dbconnect)
{
die("Unable to connect to the specified database!");
}
else
{
$auteur=utf8_decode(urldecode($_GET["artist"]));
//$auteur=utf8_decode($auteur);
$resultset = mysqli_query($connection,"SELECT * FROM $auteur");
if( $resultset )
{
$records = array();
//Loop through all our records and add them to our array
while($r = mysqli_fetch_assoc($resultset))
{
$records[] = $r;
}
$records = array_map(function($r) {
$r['artist'] = utf8_encode($r['artist']);
return $r;
}, $records);
//Output the data as JSON
echo json_encode($records);
}
else{
die("Error:".mysqli_error()); //Here is the error
}
}
}
?>
In output, I have "Error:" without any specification, so I can't know the source of the problem.
Any something strange in the php file?
Thank you for your help.
have you tried utf8mb4 ?
ALTER DATABASE database_name CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;
ALTER TABLE table_name CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
utf8mb4 is superset of utf8
Advice: database ,table names and columns you should make them in non utf8 characters.
EDIT:
to handle with utf8 characters you have to make all in utf8 such as:
your files , convert them to utf8 , all pages where you are using utf8 also that page where you geting the author fom.
database , tables , columns.
your editor where you write codes.
headers.
...
try putting the following lines just after the php tag starts and error will be displayed.
error_reporting(E_ERROR);
ini_set("display_errors",1);
hope this helps.
You are using mysqli, not mysql. So you have to use appropriate error function, and call it according to it's proper syntax.
Also, your idea on storing data in user-defined tables is wrong. You have to learn relational database basics first.
I have a problem with polish characters. I can't get correctly written words, like '?ukasz' instead of "Łukasz" or even "null", when it supposed to be "Kraków". I tried "mysql_set_charset('utf-8'/'iso-8859-1')" after mysql_connect or iconv(on json_encode($output)) and it's still the same (except now there is "Krak\u00f3" instead of "null"). I'll appreciate any help.
This is a php file for my Android app:
$id_client = $_REQUEST['id_klienta'];
$con=mysql_connect('localhost', 'root', '') or die(mysql_error());
mysql_select_db('courier_helper') or die(mysql_error());
$sql=mysql_query("SELECT * FROM `clients` WHERE id_klienta='$id_client'");
while($r=mysql_fetch_assoc($sql))
$output[]=$r;
print(json_encode($output));
mysql_close($con);
?>
You have to make sure, that you are using UTF-8 everywhere:
script file encoding (UTF-8 instead of ANSI) - you can set encoding it in Notepad++
html code (meta charset)
database table charset (when you are creating table or database)
database mysql_set_charset('utf8', $connection_obj);
database SET NAMES utf8 - run that SQL command after connecting
And one more thing - get familiar with PDO. This is my PDO connect function I use:
function DbConnect()
{
$db_host = "localhost";
$db_name = "database_name";
$db_user = "your_username";
$db_pass = "your_passwd";
$link = new PDO("mysql:host=$db_host;dbname=$db_name; charset=UTF-8", $db_user, $db_pass);
$link->exec("set names utf8;");
return $link;
}
You can use that function like this (this is PDO example):
$link = DbConnect();
$query = $link->prepare("SELECT id FROM wp_users");
$query->execute();
$result = $query->fetchAll(PDO::FETCH_ASSOC);
You should have your database storing data as UTF8, which means converting your existing tables.
ALTER TABLE tablename CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
You also need to make sure your connection to the database is UTF8. You can make sure of that by running a SET NAMES query right after your connect.
SET NAMES UTF8
As others mentioned, you should start using PDO.