SQL Server cyrillic error in php - php

First of all I want to apologize to my bad English.
And now for my problem. I have a SQL Server database and I want to get data from it with php. I can execute just select queries, because the database is not on localhost. I must get data from this database and the data is in cyrillic. This is my problem. When I execute mssql_query and get data, the Cyrillic symbols are returned as ????.
I try to convert characters with iconv and mb_convert_string, but without success.
Does anybody have a similar problem and how have you solved it?

Do you have the cyrillic language pack installed on the web server and your client computer? If you can connect to the SQL Server directly you might want to just export the data from the SQL Server using the BCP command line utility.
What is the end goal that you are going for?

If the returned characters are actually question marks then you the problem may have been at the DB end with the data having been incorrectly imported. In this case there may be little you can do.
If the returned characters are just being displayed as question marks, check the actual bytes (e.g. by using bin2hex on the string) and use that to determine the encoding that you have received. You'll need to inform iconv of the correct encoding otherwise its "garbage in, garbage out".

Related

Mysql dealing with non-English char input, fine with phpMyAdmin command box, not via PHP

My database deals with Chinese and Japanese characters.
When I insert rows on the phpMyAdmin command box, it works beautifully.
But problem occurs when I set up a query input on my website, the query is fetched by a php file, like:
$query = $_POST['query'];
$result = $dbc->query($query);
The non-English characters just become rubbish in database, like
ID column1
1666 ä½ å¥½å•Šå•Šå•Š
I checked that the php file receives the characters fine, the problem should come from Mysql. All charset is utf-8.
I am new to mysql, please let me know if you may need more info.
Thank you in advance.
column's collation set to utf8_general_ci
More information : https://dev.mysql.com/doc/refman/5.7/en/charset-applications.html
Add this line into your config.inc.php in PHPMyAdmin
$cfg['DefaultCharset'] = 'utf8';
$cfg['DefaultConnectionCollation'] = 'utf8_general_ci';
Since phpMyAdmin works expected, it sounds like your application is at fault here.
Perhaps you could clarify what you mean when you wrote "php file receives the characters fine" -- how did you determine that? The way I read it, you can create the proper characters from your application and they're displayed correctly in the database through phpMyAdmin, but when you try to display it through your application it shows gibberish. In that case it seems the portion of the application that retrieves and displays the data is at fault.
Regardless, there are many answers here about having gibberish characters, the best place to start is UTF-8 all the way through and you could also read more at Special characters in PHP / MySQL or Problem with PHP and Mysql UTF-8 (Special Character). Finally, even thugh it's off the Stackoverflow network, the phpMyAdmin wiki covers this pretty well also.
As a hint, most of the time I see this issue it's caused by not issuing SET NAMES (or using an equivalent function) when setting up the MySQL connection from your PHP script.

Searches for Chinese characters in Sqlite database with PHP don't match

I'm having some trouble getting PHP to match queries of Chinese characters in an SQLite database. The same search when done in sqlite3 from the command prompt yields a hit, but PHP shows nothing.
The code:
$db = new PDO('sqlite:database.db');
$sql=$db->prepare("SELECT * FROM character WHERE character='安';");
$sql->execute();
$result=$sql->fetchAll();
print_r($result);
This shows an empty array. When I search for any other field(not a Chinese character), it will show the correct result.
For laughs, I set the headers to utf-8, 16, 16BE, 16LE(just flailing), but this did nothing.
The data type for the field is set as nchar, but setting it to text gives no different result. Similarly, using SQLite3 instead of PDO makes no change.
Any ideas? Thank you in advance for any help.
By default, the Windows console does not correctly handle UTF-8 input/output.
Any data you have entered or edited in the console is some wrong encoding.
You could try to execute chcp 65001 before running sqlite3.exe, but you'd better use some other SQLite tool that handles UTF-8 correctly in the first place.
It appears that there is no easy answer, but it isn't a problem with the Windows console, as the database handles all input, from browser or console in the same manner. Despite sources saying that sqlite can use utf-8, it appears to work in ucs-2, an encoding I have no knowledge of. I verified this by decoding and encoding ucs to utf.
The easy answer, and the one I took, as I couldn't get the encoding to jive, was to just use base64_encode on queries going into the database, and decode on information coming out. It's not elegant, but it allows utf characters into a non-utf database.
http://jp2.php.net/manual/en/function.base64-encode.php

PHP mysql fixed connection to utf8, but now existing greek data is useless

I have a mysql database storing some fields in greek characters. In my html I have charset=utf-8 and my database columns are defined with encoding utf_general_ci. But I was not setting the connection encoding so far. As a result I have a database that doesn't display the greek characters well, but when reading back in PHP, it all shows well.
Now I try to do this the right way, so I added also in my database functions.
$mysqli->set_charset("utf8");
This works great for new entries.
But for existing entries, the problem is that when I read data in PHP, it comes garbled, since now the connection encoding has changed.
Is there a way to fix my data and make them useful again? I can continue working my old way, but I know it's wrong and can cause me more problems in the future.
I solved this issue as follows:
in a PHP script, retrieve the information as I do now, i.e without setting the connection. This way the mistake will be inverted and corrected and in your php file you will have the characters in the correct utf-8 format.
in the same PHP script, write back the information with setting the connection to utf-8
at this point the correct characters are in the database
I changed all my read/write functions of your site to use the utf-8 from now on

How to get correct character-encoding between mysql and filemaker

I'm unsure if this is a php-, filemaker-, mysql- or an odbc driver issue.
For security reasons the input fields of my current php webform convert special characters into hex codes, (for example: # becomes ' ) This hex code is saved in the database and will also be shown in Filemaker11 as the hex code. This is not what i want.
How can I make sure the special character will be displayed as it should be?
The other way round (from filemaker to db), no conversion will be done on inserting the special characters.
How can I make sure everything will be consistent?
Kind regards,
Jeroen
FileMaker is just showing the data stored in MySQL. If you pull up the DB in a tool like PhpMyAdmin you should see that the varchar contains the encoding as well. Since FMP is looking at it simply as a text field, it shows the encoding that was stored. If you wanted to decode in FMP you could show a calc field of the varchar that has a custom function to decode the text. (but that won't allow for updating the data..) You could also try a trigger on record load to decode the data in the fields so that you can properly view/edit.
Solved it! It appeared that I had to add an extra line to my PHP script.
after setting up the connection, php needs to tell mysql what the encoding needs to be. This can be done with the following line:
$dbh->query("SET NAMES 'utf8'");
Thanks for the effort guys!
This: ' type of encoding is not done automatically by the browser. Something is doing it. Normally you do it only on output not on input.
You can use html_entity_decode() to undo it. But I strongly suggest you figure out why it's happening in the first place.

Error converting client characters into server's character set?

I have designed a web form using PHP as a server-side script that should insert data into a Sybase ASE database table using ODBC Functions.
When I fill the form fields with English word and ASCII Characters it works ok and saves data in the database but when I use Arabic and extended ASCII, UTF, or Unicode characters, I get the following error message:-
Warning: odbc_exec()
[function.odbc-exec]: SQL error:
[INTERSOLV][ODBC SQL Server
driver][SQL Server]Error converting
client characters into server's
character set. Some character(s) could
not be converted. , SQL state S1000 in
SQLExecDirect in
C:\wamp\www\website1\webpage1.php on
line 111
Is this because I have the settings on my database (or server) set incorrectly? In which case what should I change? and how do I change it?
Or, do I need to some function(s) to convert the extended ASCII characters? In which case, have the necessary functions already been writeen? and where can I find them?
How I can solve that problem?
Thanks for your help.
I discovered the solution of the problem afterwards which was that I am not saving the php file in the correct encoding using Save As and choosing the right encoding.
I would like to share that so that it may be helpful for others who had the same problem.
Sometimes We face problems that seems to be very difficult but their solutions are very simple as we discovered afterwards.

Categories