LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-17-2011, 08:53 PM   #1
gozlemci
Member
 
Registered: Mar 2010
Location: Turkey
Distribution: Ubuntu 12.04
Posts: 34

Rep: Reputation: 0
Turkish Character Issue in PHP and MS-SQL


Hi There;
As a hot volunteer of an open source initiative, I feel sorrow using MS-SQL and IIS. I could not convince boss to use mySQL, but anyway I can push PHP on boss.

I have a MS-SQL server and I have Turkish names in my table. User enters names and I send it to SQL server, print result to output. However, my PHP code process and prints text as a UTF-8, and send it to MS-SQL and print it to output.However data in MS-SQL is in Windows(Turkish) format, so there are nothing to return. For example;
when user types "Güray" in html input form, PHP issues a query:
"SELECT * FROM table WHERE name ='Güray' " and sends it to MS-SQL server. Server returns nothing, but I know that there are many entries that name attribute is 'Güray'.
But when name contains purely English characters, such as 'Ali' it works fine.

In addition, when I print result of query returned by MS-SQL via HTML , Turkish character in a string printed in a rectangle.

How can I overcome these two frustrating situations? Any help is greatly appreciated.

Last edited by gozlemci; 11-17-2011 at 09:02 PM.
 
Old 11-18-2011, 05:36 AM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,630
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
You probably want to look at the PHP function mb-convert-encoding.

Also, what would happen if someone entered "Güray'; DROP TABLE table; --" as their name?
 
Old 11-18-2011, 06:10 AM   #3
gozlemci
Member
 
Registered: Mar 2010
Location: Turkey
Distribution: Ubuntu 12.04
Posts: 34

Original Poster
Rep: Reputation: 0
Thank you for your suggestions, I will be considering your function.
If the thing you've said will happen , it would be a great catastrophe
 
Old 11-19-2011, 06:30 AM   #4
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,630
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
Quote:
Originally Posted by gozlemci View Post
If the thing you've said will happen , it would be a great catastrophe
Exactly It might be a good idea to read up on preventing SQL injection attacks (there are many other sites on the web, which could be better than the one I found - search for 'preventing SQL injection' or similar in Google)
 
Old 11-19-2011, 10:00 PM   #5
gozlemci
Member
 
Registered: Mar 2010
Location: Turkey
Distribution: Ubuntu 12.04
Posts: 34

Original Poster
Rep: Reputation: 0
I can not even solve the problem. Any help is greatly appreciated.Here is the part of the code :

This code creates SQL word and connects to the database.The odbc_result_all_ex (phrase in red) prints the result.
Code:
 $virtual_dsn = 'DRIVER={SQL Server};SERVER=REHAB-EB07315EE\SQLEXPRESS;DATABASE=osman';
    $connection = odbc_connect($virtual_dsn,'sa','123456') or 
        die('ODBC Error:: '.odbc_error().' :: '.odbc_errormsg().' :: '.$virtual_dsn);

    /*
     * EN : Connecting to the database.
     */
    odbc_exec($connection,'osman');
    
    //# For experimental purposes
    echo $SQLSentence;
   
     /*
     * EN : Query is sending.
     */
    $result = odbc_exec($connection , $SQLSentence);

    if(!$result)
    {
        echo "Query failed " .odbc_error();
    }
    
    odbc_result_all_ex($result,'Border=1 cellspacing=1 cellpadding=5', "style='FONT-FAMILY:Tahoma; FONT-SIZE:9pt; BORDER-BOTTOM:solid 1pt gree'");

    sqlsrv_close($connection);
Contents of odbc_result_all_ex.The red is the nub:

Code:
       function odbc_result_all_ex($res, $sTable, $sRow)
    {
        $cFields = odbc_num_fields($res);

        $strTable = "<table $sTable>";
        $strTable .= "<tr>";
        
        for ($n=1; $n<=$cFields; $n++)
        {

            $strTable .= "<td $sRow><b>". str_replace("_", " ", odbc_field_name($res, $n)) . "</b></td>";

        }
        $strTable .= "</tr>";

        $temp;
        while(odbc_fetch_row($res))
        {
            $strTable .= "<tr>";
                for ($n=1; $n<=$cFields; $n++)
                {
                    if (odbc_result($res, $n)=='')
                    {
                        $strTable .= "<td $sRow>&nbsp;</td>";
                    }
                    else
                    {
                        $temp = mb_convert_encoding(odbc_result($res, $n), "UTF-8");
                        $strTable .= "<td $sRow>" . $temp . "</td>";
                    }

                }
            $strTable .= "</tr>";
        }

        $strTable .= "</table>";

        echo $strTable;

    }
I tried every encoding recognized in PHP, but I couldn't able to print Turkish characters properly.
Here is the code that lists the encoding:

Code:
$text = "Pijamalı hasta yağız şoföre çabucak güvendi."; //Turkish "The quick brown fox jumps over the lazy dog"; 

foreach(mb_list_encodings() as $chr){ 
        echo mb_convert_encoding($text, $chr)." : ".$chr."<br>";    
 }
Any help is greatly appreciated. I am in trouble.
 
Old 11-20-2011, 07:43 AM   #6
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,630
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
Only thing I can think of is to use HTML entities:

Code:
â = &acirc;
 = &Acirc;
ç = &ccedil;
Ç =&Ccedil;
ğ = ğ
Ğ = Ğ
ı = ı
İ = İ
ö = &ouml;
Ö = &Ouml;
ş = ş
Ş = Ş
ü = &uuml;
Ü =&Uuml; 	  	 
î = &icirc;
Î = &Icirc;
û = &ucirc;
Û = &Ucirc;
You can then search through all your text, replacing any special characters with the above entities, which should then appear correctly regardless of character set. If you're doing text comparison with your database, you'll also need to convert all your database data to use these entities as well

Hope this helps,
 
  


Reply

Tags
encoding, php


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Turkish Character Issue in PHP and MS-SQL gozlemci Programming 1 11-18-2011 09:31 PM
SQL substring matching a character ShaqDiesel Programming 2 04-07-2011 02:53 PM
PHP and ms-SQL joelhop Programming 2 11-22-2006 08:45 AM
mount -t smbfs -o // turkish character problem fahridon Linux - Networking 1 05-23-2006 05:33 AM
Php, Sql kotoisin Fedora 3 11-30-2004 05:18 PM


All times are GMT -5. The time now is 09:04 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration