Character Set issue using SQL Server and ODBC in php - php

I connected to to sqlsever2008 with odbc(with dsn) and php but problem is that when I retrieve information from that it has character like ���������. My datatype in sqlserver is nvarchar. Any idea why it is happening?
NOTE: The page is utf8.
Edit:
I use echo utf8_encode(odbc_result($this->result,'name') ); and characters are like:
ÂãæÒÔ ÒÈÇäÂãæÒ
i retrieve information like this:
odbc_result($this->result,'name')

Try to use utf8_decode() or utf8_encode().

It seems, the code page your sql server has been installed is different with your native language. perhaps you need to reinstall the sql server with correct code page.

i found the problem.
iconv("Windows-1256", "UTF-8", "$temp")

Related

PHP & OCI query returns NUMBER columns as STRING

I'm using PHP5 and OCI 8 with Oracle 11g.
When I fetch a row using oci_fetch_all, the whole result is converted as STRING even for the NUMBER columns and even if I use Oracle's TO_NUMBER in the query.
What I'm trying to do is simple: the javascript calls the PHP script through an Ajax request. The script just fetch some NUMBER data and encode them into JSON. I want the data to be encoded as integer, so the javascript can do math stuff on it (add, divide,..etc) without any conversion.
I am pretty sure that the problem comes from OCI and not JSON encoding because when I VAR_DUMP the result of oci_fetch_all, I can clearly see double quotes on every result:
{
"COLUMN1":"12",
"COLUMN2":"52"
}
I want the result to look like this:
{
"COLUMN1":12,
"COLUMN2":52
}
I tried to:
Change the flag of oci_fetch_all (OCI_FETCHSTATEMENT_BY_ROW, OCI_FETCHSTATEMENT_BY_COLUMN...)
Use oci_fetch_array instead of oci_fetch_all
Remove the UTF8 encoding on the connexion to oracle (I know, its stupid)
The strange thing is that I can't find any thing on the internet about this problem... It's like nobody faced the same issue. Maybe i'm doing something wrong...
Thanks in advance
You can use an extra option in json_encode:
json_encode($rows, JSON_NUMERIC_CHECK);
However this option requires a PHP version of 5.3.3 or higher (thus its ok for you).
All database extensions in PHP work like this, there's nothing you can do about it.
You'll have to manually type-cast the database results.

PHP / MySQL special characters issue

I'm facing an issue with the special characters. I'm taking information from a DB in MSSQL which returns in php a value which may contain specials characters like "à é ö ü" etc. In my sample, I will use the city name of Zürich and when I try to insert this information into a MySQL database, I get the following error :
"Incorrect string value: '\xFCrich ...' for column..."
so, I've done the following but it still showing the same error message:
$arrSearch = array('\xE4','\xF6','\xFC','\xC4','\xD6','\xDC','\xDF');
$arrReplace = array('ä','ö','ü','Ä','Ö','Ü','ß',);
$City=str_replace($arrSearch, $arrReplace, $City);
If I do an echo of $City, I get the following :
Z�rich (rectangular block)
I've tried as well hex2bin() but I just get a white page and nothing is inserted into Database. FYI, DB collation is in utf8mb4_general_ci and setlocale(LC_ALL, 'en_EN') is set in php file. All php files are encoded into UTF8 and chatset is set as follow : mysql_set_charset('utf8mb4',$link);
I must admit, I'm a bit lost. Does anyone has a clue on how to fix this?
Thanks.
EDIT: The server hosting this app is running under 2008R2/IIs 7.5 and I've found this KB by Microsoft. I'll try the hotfix and the registry modification but it didnt work. http://support.microsoft.com/kb/2277918/
Set the character set to utf8.
Ok got it! Was so stupid.... I'm using FPDF with that insertion and to show special characters properly in FPDF, I had to set an iconv('UTF-8', $charset, $_REQUEST['City']);
Sorry and thanks again for assistance! now works like a charm

Unable to save text to database with 'body' => htmlentities($_POST['body']) on server, but on localhost it works

I have an array called $data and I am sending this data to my db insert method.
This works nice when used with:
'body' => $_POST['body']
However, when I try:
'body' => htmlentities($_POST['body'])
Only a first 3 words are saved but when it comes to character ě it stops there and other parts of the text is not saved.
in my db body is stored in body column which is a standard text type, utf_8_general_ci.
It has nothing to do with db settings I guess, because all tables is set to uf8_general_ci as well as the table itself (the same as on my localhost).
The funniest part is that on my localhost machine running wamp it wokrs with al these weird characters and the text is saved correctly when sing htmlentities.
Only on the server it didn't work. Btw. it's justhost if it helps.
Do you know how to run it the same way as on my localhost or any other function that can be used except htmlentities ?
I need htmlentities because I have html code like google maps and it needs to be saved. Without htmlentities it is saved incorrectly.
Try using mysql_real_escape_string or mysqli_real_escape_string or prepared statements when inserting into a DB.
Try and set the htmlentities encoding to match your form data encoding.
Note the dividing PHP version 5.4.0: default encoding for htmlentities can be UTF-8 or ISO-8859-1. Does your local machine have the same PHP version as server?
Note to all local machine users (MAMP, WAMP, LAMP, whatever): always check your PHP/MySQL versions if they match the server version. There is almost always some common function that doesn't work as expected when the testing environment isn't identical to production environment (been there, done that – once or twice...).
You can use set utf8 to mysql before insertion. It can insert special character in the database. FYI http://php.net/manual/en/function.mysql-set-charset.php

encode to arabic proplem

i need change encode to windows-1256 like this:
print utf8_encode($text)
not ture:
print windows-1256_encode($text)
I had almost the same problem as you do where I wanted to retrieve Arabic data fields from SQL Server 2008 R2 with php using ODBC connection and then I found that getting the right information depends on using " iconv " like this:
echo iconv("","utf-8",$result);
So you can see I didn't mention the first encoding but the way I wanted to display my Arabic data (utf8).
did you try the function mb_convert_encoding
see:
http://www.php.net/manual/en/function.mb-convert-encoding.php and
http://www.php.net/manual/en/mbstring.supported-encodings.php
Alternatively to Stewie's answer you can use the iconv functions.

how to avoid encoding problems using ajax-json and php-postrgesql

i have problems with encoding when using json and ajax.
chrome and ie encode umlauts as unicode when jsonifying, firefox and safari returning those utf-8 escaped umlauts like ¼Ã.
where is the best place to give all the same encoding?
js / php-get or by writing them to the database.
and i think the next trouble is, when i reload the utf-8 encoded stuff from the db, and write them to the browser and then rewrite them to the db, again via ajax-request a get a real chaos?
can i avoid the chaous? can i handle the encoding in an easy way?
pls. help :-)
very important is to also provide security
You must set everything to UTF-8, this means :
Database collation
Table collation
Field collation
Your coding software (example notepad++) encryption.
Had a similar problem. Maybe you are actually interpreting encoding the wrong way, clientwise. Try setting the frontend encoding before your queries.
<?php
$connection = pg_pconnect("dbname=data");
pg_set_client_encoding($connection, "encoding goes here"); //check enconding aletrnatives on PostgreSQL
$result = pg_query($connection, "SELECT whatever FROM wherever");
//and so on...
?>
I'm a newbie, but it may help. Also won't affect security in anyway if you are already protected against db injection.
Cheers

Categories