2

This is my code:

  odbc_exec($conn,"SET client_encoding='UTF-8'");   
  $viewTablo= odbc_exec($conn,"SELECT A FROM B WHERE A='ÇŞÖ'");

Odbc execute doesn't work with that 'ÇŞÖ' or any other Turkish character. How do I fix this?

Edit: I cannot change anything inside the database.

Code below is working.

 else if ($i==23 && odbc_result($viewTablo,$i)=="(Seçilmemiş)")
     {
    echo '
                          <td>
    <p class="text-xs text-center font-weight-bold mb-0"></p>
                         
                          </td>';              
   

Seems like it brings the result in utf-8 (or anything working) ,but anything I type on the .php file / input given inside the odbc_exec is not utf-8 (or whatever it needs) .

Besides, queries are working on the database itself.

I am open to any alternative to insert a 'ÇŞÖ' as parameter to database.

Thanks in advance.

wittream
  • 185
  • 11
  • I guess it's PHP -> tag added. Please fix it if I assumed incorrectly. – Sandra Rossi Mar 07 '23 at 08:31
  • 1
    Your PHP file must be saved in UTF-8, too. Read [UTF-8 all the way through](https://stackoverflow.com/questions/279170/utf-8-all-the-way-through). – AmigoJack Mar 07 '23 at 15:48
  • I did the most of that suggestions, I cannot change anything inside the php.ini, because I am not authorized to stop apache.But I might access to admin if it is the only solution... And mb codes are deprecated, I tried iconv instead of them. Thank you.@AmigoJack – wittream Mar 08 '23 at 05:51
  • As mentioned above, check your PHP file is saved in UTF8. Also verify the charset and collation of the database/table/field? `CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci` is generally the best, but you can get away with `CHARSET=utf8 COLLATE utf8_unicode_ci` if you don't need emoticons and some more complex languages. Also verify your collation is `ci` (case insensitive) and not `c` (case sensitive). You can also check out `ai` (accent insensitive) as well. More light reading: https://dev.mysql.com/doc/refman/8.0/en/charset.html – Robbie Apr 17 '23 at 03:15
  • @Robbie tried all of them but nothing worked – wittream Apr 17 '23 at 06:16
  • Which version of php & odbc are you using? – Mitali Patel Apr 17 '23 at 10:35
  • php 8.2, odbc 3.50 @MitaliPatel – wittream Apr 18 '23 at 06:06
  • 1
    issue is with the encoding settings for the ODBC connection. You can try changing the encoding settings for the ODBC connection to support Turkish characters. In the "Connection" tab, under "Advanced Settings", set the "Code Page" to "1254" (which is the code page for Turkish characters). – Noor Fahad Apr 19 '23 at 05:41
  • @NoorFahad thank you but I couldn't find where to change that, I have only "Odbc data sources" as an executable file. – wittream Apr 20 '23 at 05:34
  • 1
    in your connection string add: `ClientCharset={UTF-8}`. for example: `$connection_string = "DRIVER={FreeTDS};SERVER={$serverip};Port=1433;DATABASE={$dbname};ClientCharset={UTF-8}";` – Alive to die - Anant Apr 21 '23 at 05:19

4 Answers4

0

Can you try with the following added?

odbc_exec($conn, "SET CLIENT_ENCODING='UTF-8'");
odbc_exec($conn, "SET UNICODE_ENCODING=AL32UTF8");
Sander Toonen
  • 3,463
  • 35
  • 54
0

Have you checked that the database, database table, and the column are all UTF-8? This is crucial for it to work. These two queries does that:

ALTER DATABASE databasename CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

ALTER TABLE `tablename`
MODIFY COLUMN `colname` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
  • Note that "text" in the second query should be the datatype you use in this column, as it could easily be varchar(255)

Also, the file has to be UTF-8, and the old trick of using Notepad in windows to open the file and then go to File->Save as..->Choose UTF-8 in the "Ecoding" dropdown that should be on the left side of the "Save" button.

Another thing that may be just an oversight, is that in your query you use A='ÇŞÖ' meaning it has to be exactly that, but it seems better here to use "A LIKE '%ÇŞÖ%'"

Edit:

For HANA, the SQL is similar:

ALTER DATABASE <databasename> SET UNICODE ENCODING UTF8 COLLATION UNICODE_CI;

ALTER TABLE <tablename> ALTER (<colname> NVARCHAR(100) COLLATE UNICODE_CI);

Just make sure the NVARCHAR lenght is the same as your current.

John
  • 158
  • 1
  • 5
0

I have not used hana but based on some searches these are things are found that may work.

try these strings as query and see if they work.

$queryString = "SELECT A FROM B WHERE A='ÇŞÖ'";
$queryString2 = utf8_encode($queryString);
$queryString3 = utf8_decode($queryString);

if you can change column type you can check this: insert a UTF-8 value into a SAP HANA database

Also from back of my head you can check if your php file has BOM enabled. https://stackoverflow.com/questions/2223882/whats-the-difference-between-utf-8-and-utf-8-with-bom#:~:text=There%20is%20no%20official%20difference,string%20from%20the%20file%2Fstream.

Sina R.
  • 1,781
  • 2
  • 19
  • 37
0

Have you tried manually specifying a connection string (DSNless) using the necessary values from the specification for the database?

PHP will accept a DSNless connection, directly entered into the first argument of the odbc_connect command, as per the documentation.

From there, compare notes with the documentation for the connection for SAP HANA to build the connection string. You should be able to set your language and character set details directly.

Paul
  • 4,160
  • 3
  • 30
  • 56