I've done this before in .Net but I am somewhat new to PHP and could use some help. I'm trying to pull data in PHP from a MSSQL db, some of which is Greek letters. The data type of the MSSQL columns is nvarchar and the Greek characters are displayed correctly in management studio; however, whenever I pull the data with the sqlsrv_query function in php I get a bunch of question marks.
In sqlsrv_connect function I've tried setting CharacterSet"=>"UTF-8 which doesn't work.
I've tried iconv(), but am not sure which sets to use.
I tried mb_detect_encoding() and it returned ASCIIASCII.
Here is some sample code:
public function _getOrderItemDetails($orderItemIds,$productTypeId){
$params = explode(',', $orderItemIds);
$sql = "select od.*, ofs.Sort
from orderDetail od
left join orderFieldSort ofs on ofs.fieldName = od.name and productTypeId in ($productTypeId)
where orderItemid in (".rtrim(str_repeat('?,', count($params)),',').")
order by ofs.sort";
return $this->_compileResults($sql,'OrderDetail',$params);;
}
private function _compileResults($sql,$classType,$params ){
$result = sqlsrv_query($this->conn,$sql,$params);
$arrayResult = array();
while($orderObject = sqlsrv_fetch_object($result,$classType)){
array_push($arrayResult, $orderObject);
}
return $arrayResult;
}
Any suggestions are welcome. Please help!
A couple of things to maybe try.
1) Can you validate exactly what character encoding your DB is in? I recommend UTF8 for everything as it should handle the widest range of characters. "ASCIIASCII" is not a character encoding to my knowledge do you mean "ASCII"?
2)Try using mysql_set_charset('utf8'); changing the char set to match your db encoding. It "Sets the default character set for the current connection." from within the current php script. PHP official docs
3)This is an outlier but where are you viewing the results? Irrelevant of the data returned from your DB the if you are viewing the results in a web browser you will need to make sure the page has the appropriate charter encoding set in the head if this is a miss match from the character set returned from your db it can result in the question marks or diamonds... Typically something like this
<head>
<title>page title</title>
<meta charset="UTF-8" />
</head>
I had exactly the same problem with you so when i was searching to find the solution i saw your question.The answer is very simple.I guess somewhere in your code you connect to the database with code like the following :
$serverName = "YOUR_SERVER";
$connectionInfo = array("Database"=>"YourDatabase");
$link = sqlsrv_connect($serverName,$connectionInfo);
if(!$link ) {
die('Could not connect: ' . mysql_error());
}
To make greek characters show up you need to change line 2, from:
$connectionInfo = array("Database"=>"YourDatabase");
to :
$connectionInfo = array("Database"=>"YourDatabase","CharacterSet"=>"UTF-8");
and the rest code stays the same.
This solution was suggested by #Graham to his own question i think, and it is from here : Greek character insertion in php compared to SQL server management studio
Related
I am trying to fetch our Arabic values from JDE Database using the following connection string:
$dsn = "Driver={SQL Server};Server=10.10.10.27;Database=JDE;charset=utf8";
$username = "username";
$password = "password";
$string = "odbc:".$dsn.";Uid=".$username.";Pwd=".$password.";";
$con = new PDO($string);
As you can see I have the charset=utf8 specified.
I also have my HTML meta present:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
I also have used arabic characters fetched from MySQL before and it is displayed correctly and I can see Arabic characters, but from JDE database I get the following:
name: ??? ???? ???? ??? ??? ??? ???? ???? ??? ???
I tried adding the following in my php code trying different output:
echo iconv('windows-1256', 'utf-8', $DataFromJDE);
echo utf8_decode($DataFromJDE);
echo utf8_encode($DataFromJDE);
But all failed.
Is there a configuration I need to do on the server?
I am using Apache with PHP 7 on a Windows server.
The JDE is in a separate server.
What am I missing? Is it from PHP or JDE?
Try something like:
echo $DataFromJDE . '<br>';
Note that you don't need to use utf8_decode(...) or anything similar, browsers support UTF-8 as is, without any need for reformatting.
YES!
Finally Cracked the code!
Thank you So much for the amazing explanations and clarifications. your answers helped a lot.
So basically what I had to do is.
Change my query to the following
$con->prepare('SELECT CAST( CAST(JDEColumn AS VARBINARY(MAX)) AS varchar(120)) AS JDEColumn FROM TABLE where COLUMN = :SELECTION');
courtesy of this thread here https://stackoverflow.com/a/29975739/11835221
Using iconv() as follows:
iconv('UTF-16LE', 'utf-8', $DataFromJDE);
got the clue from this answer by reverse engineering a bit https://stackoverflow.com/a/51130953/11835221
AND Voila! I got my Arabic data!
Hoping this helps another hair pulling soul some day.
Cheers!
I'm trying to do a select from a DB2 through PHP and odbc and then save those values on a file. The OS where the code is being executed is Debian. What I do is the following:
$query = "SELECT NAME FROM DATABASE_EXAMPLE.TABLE_EXAMPLE";
$result = odbc_prepare($server, $query);
$success = odbc_execute($result);
$linias = "";
if ($success) {
while ($myRow = odbc_fetch_array($result)) {
$linias .=format_word($myRow['NAME'], 30) . "\r\n";
}
generate_file($linias);
function format_word($paraula, $longitut) {
return str_pad(utf8_encode($paraula), $longitut, " ", STR_PAD_LEFT);
}
function generate_file($linias) {
$nom_fitxer = date('YmdGis');
file_put_contents($nom_fitxer . ".tmp", $linias);
rename($nom_fitxer . '.tmp', $nom_fitxer . '.itf');
}
The problem is that some of the retrieved values contains spanish letters and accents. To make and example, one of the values is "ÁNGULO". If I var_dump the code on my browser I get the word fine, but when it's write into the file it apends weird characters on it (that's why I think there is a problem with the charset). I have tried different workarounds but it just make it worst. The file opened with Notepad++ (with UTF8 encoding enabled) looks like:
Is there a function in PHP that translate between charsets?
Edit
Following erg instructions I do further research:
The DB2 database use IBM284 charset, as I found executing the next command:
select table_schema, table_name, column_name, character_set_name from SYSIBM.COLUMNS
Firefox says the page is encoded as Unicode.
If i do:
var_dump(mb_detect_encoding($paraula));
I get bool(false) as a result.
I have changed my function for formating the word hoping that iconv resolve the conflict:
function format_word($paraula, $longitut) {
$paraula : mb_convert_encoding($paraula, 'UTF-8');
$paraula= iconv("IBM284", "UTF-8", $paraula);
return $paraula;
}
But it doesn't. Seems like the ODBC it's doing some codification bad and that is what mess the data. How can I modify the odbc to codificate to the right charset? I have seen some on Linux changing the locale, but if I execute the command locale on the PC I get:
LC_NAME="es_ES.UTF-8"
LC_ADDRESS="es_ES.UTF-8"
...
I will try to summarize from the comments into an answer:
First note that PHPs utf8_encode will convert from ISO-8859-1 to utf-8. If your database / ODBC-Driver does not return ISO-8859-1 encoded strings, PHPs utf8_encode will fail or return garbage.
The easiest solution should be to let the database / driver convert the values to correct encoding, using its CAST function: https://www.ibm.com/support/knowledgecenter/SSEPEK_11.0.0/sqlref/src/tpc/db2z_castspecification.html
Try to alter your query to let DB2 convert everything to UTF-8 directly and omit the utf8_encode call. This can be done by altering you query to something like:
SELECT CAST(NAME AS VARCHAR(255) CCSID 1208) FROM DATABASE_EXAMPLE.TABLE_EXAMPLE
Thanks to Sergei for the note about CCSID 1208 on IBM PUA. I changed CCSID UNICODE to CCSID 1208.
I do not have DB2 at hand here, so the above query is untested. I'm not sure if this will return utf-8 or utf-16..
I am working for a company who bought DBA Manufacturing and I need to retrieve some values from the Pervasive SQL 2000i DB. Unfortunately there are no plans to upgrade the DB so I have to make due with what I have. I am running a 32 bit
I was able to make an odbc_connect call in PHP to the DSN on a windows machine. I am running PHP on Windows with NGINX.
<html>
<head>
<title>
Placeholder Title
</title>
</head>
<body>
<?php
// mb_internal_encoding("UTF-8");
// connect to the dbamfg DSN
$connect=odbc_connect("Driver={Pervasive ODBC Client Interface};ServerName=dbamfg.1583;ServerDSN=dbadefault;","","");
// query the users table for name and surname
$query = "select * from BKARINVL where BKAR_INVL_INVNM = 600100 order by bkar_invl_cntr ";
//Prepare the statement
$result = odbc_prepare ($connect, $query);
// execute the query
odbc_execute($result);
// fetch the data from the database
while(odbc_fetch_row($result)){
$SalesOrder = odbc_result($result, 1);
$InvLine = odbc_result($result, 2);
$EstShpDt = odbc_result($result, 3);
$ProdCode = odbc_result($result, 4);
$ProdDesc = odbc_result($result, 5);
echo ('<br />'.$InvLine.'<br />');
echo ($SalesOrder.'<br />');
echo ($EstShpDt.'<br />');
echo ($ProdCode.'<br />');
echo ($ProdDesc.'<br />');
}
// closing the connection
odbc_close($connect);
?>
</body>
</html>
The query returns numeric values just fine. When it comes to text columns it returns 10 characters and after that it shows characters that are not displaying correctly.
here is an example:
1
600100
2015-05-31
020-0015-1
DIGITAL VI!2S��
2
600100
2015-05-31
020-0012-0
2 X 4 AUDI!5S��
3
600100
2015-05-31
020-0065-0
16-CKT LOW!8S��
I'm excited and shocked that I have gotten this far! It's exceptionally fast to retrieve values from Pervasive through the ODBC connection and I feel like I am very close to being successful. I just need a push in the right direction.
Everything I have tried so far: I have tried to set the internal encoding of the page to several different character sets including UTF-8, Windows, ASCII, etc. I have also tried to select the checkbox under the ODBC driver setup options during DSN setup in Windows, which says "Use OEM/ANSI Conversion". This does nothing but change the characters by a bit.
Another clue to this is that for some reason when I call the connection string using System.Data.odbc in C# it returns the query and text values in full.
I have tried using iconv to convert the values to UTF-8, etc., to no avail. I've tried forcing the header to display the page in different character sets. I have tried mb_detect_encoding() on the odbc_result() and the return value says UTF-8, but I assume it has to do There must be someone out there that knows how to fix this. Please, would anyone point me in the right direction? I will be forever grateful!
If anything more needs to be added to solve this I will include it when requested. Thank you!
I have one php form where i used to enter data to database(phpmyadmin), and i used SELECT query to display all values in database to view in php form.
Also i have another PHP file which i used to create JSON from the same db table.
Here when i enter foreign languages like "Experiența personală:" the value getting saved in DB is "ExperienÈ›a personală: " but when i use select query to display this in same php form it coming correctly "Experiența personală:". So the db is correct and now am using following php code to create JSON
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "aaps";
// Create connection
$con=mysqli_connect($servername,$username,$password,$dbname);
// Check connection
mysqli_set_charset($con, 'utf8');
//echo "connected";
$rslt=mysqli_query($con,"SELECT * FROM offers");
while($row=mysqli_fetch_assoc($rslt))
{
$taxi[] = array('code'=> $row["code"], 'name'=> $row["name"],'contact'=> $row["contact"], 'url'=> $row["url"], 'details'=> $row["details"]);
}
header("Content-type: application/json; charset=utf-8");
echo json_encode($taxi);
?>
and JSON looks like
[{"code":"CT1","name":"Experien\u00c8\u203aa personal\u00c4\u0192: ","contact":"4535623643","url":"images\/offers\/event-logo-8.jpg","details":"Experien\u00c8\u203aa personal\u00c4\u0192: jerhbehwgrh 234234 hjfhjerg#$%$#%#4"},{"code":"ewrw","name":"Experien\u00c8\u203aa personal\u00c4\u0192: ","contact":"ewfew","url":"","details":"eExperien\u00c8\u203aa personal\u00c4\u0192: Experien\u00c8\u203aa personal\u00c4\u0192: Experien\u00c8\u203aa personal\u00c4\u0192: "},{"code":"Experien\u00c8\u203aa personal\u00c4\u0192: ","name":"Experien\u00c8\u203aa personal\u00c4\u0192: ","contact":"","url":"","details":"Experien\u00c8\u203aa personal\u00c4\u0192: "}]
In this "\u00c8\u203aa" this is wrong it supposed to be "\u021b" (t).
So pho used to creating JSON making this issue.
But am unable to find exactly why its coming like this . please help
Avoid Unicode -- note the extra argument:
json_encode($s, JSON_UNESCAPED_UNICODE)
Don't use utf8_encode/decode.
ă turning into ă is Mojibake. It probably means that
The bytes you have in the client are correctly encoded in utf8 (good).
You connected with SET NAMES latin1 (or set_charset('latin1') or ...), probably by default. (It should have been utf8.)
The column in the tables may or may not have been CHARACTER SET utf8, but it should have been that.
If you need to fix for the data it takes a "2-step ALTER", something like
ALTER TABLE Tbl MODIFY COLUMN col VARBINARY(...) ...;
ALTER TABLE Tbl MODIFY COLUMN col VARCHAR(...) ... CHARACTER SET utf8 ...;
Before making any changes, do
SELECT col, HEX(col) FROM tbl WHERE ...
With that, ă should show hex of C483. If you see C384C692, you have "double-encoding", which is messier to fix.
Depending on the version of MySql in the database, it may not be using the full utf-8 set, as stated in the documentation:
The ucs2 and utf8 character sets do not support supplementary characters that lie outside the BMP. Characters outside the BMP compare as REPLACEMENT CHARACTER and convert to '?' when converted to a Unicode character set.
This, however, is not likely to be related to your problem. I would try a couple of different things and see if it solves your problem.
use SET NAMES utf-8
You can read more about that here
use utf8_encode() when inserting data to the database, and utf8_decode() when extracting. That way, you don't have to worry about MySql manipulating the unicode characters. Documentation
I've connected my php webpage to my .mdb database via odbc connection.
My first question is:
What should I do to avoid ???? instead of proper Persian characters, when displaying my db table rows on the page.
My sample html code:
<form action="" method="post">
<input type="text" value="my_value">
</form>
My sample PHP code:
<?php
$conn = odbc_connect('my_db','','');
if (!$conn) {
exit("Connection Failed: " . $conn);
}
$submitted_value = $_POST["my_value"];
$sql = "SELECT * FROM my_table WHERE column1 = '$submitted_value'";
$result = odbc_exec($conn,$sql);
odbc_close($conn);
?>
My charset is already set to UTF-8 in php.ini.
And my second question is: (which I think might be because of the above problem):
When I type Persian in the html input value tag (instead of "my_value") and already have a row with exactly the same value in column 1, nothing is returned.
But When I change both value tag name and column 1 value of the table row to English. The result is returned.
Can anyone help me with this? I appreciate in advance.
Check to make sure your database is also configured to store UTF8, some only do ASCII. Also, when displaying, make sure the HTML document is configured to display UTF8.
Example:
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-6">
source: http://www.webdeveloper.com/forum/showthread.php?224111-Can-t-see-my-persian-arabic-text-in-HTML-editor