Log in

View Full Version : Wrong characets in MS SQL output



Chewbacca
07-09-2010, 12:49 PM
I am a beginner, and learn as I make progress.
I have successfully managed to retrieve data from my MS SQL server, but the output does not display the special characters right ( Swedish ).

I have stated that the document should be utf-8 and the text above the database output comes out right, but not from the SQL Database.

Output:

Ansluten till Databasen.
Nu är jag ansluten till databasen... Skickar en fråga till SQL-databasen...
Antal funna rader: 10

* 1234567 TEST TESTSSON Test S�GV�GEN 1 STOCKHOLM 019-20 53 39 TURE TESTSSON
* 1900000 ANDREAS STIHL NORDEN AB GLIMMERV�GEN 6 STENKULLEN
* 1900001 MALMBY MOTOR AB MALMBYV�GEN 2 STR�NGN�S 0152-250 72 JIMMY BJUR
* 1900002 HEDMANS MOTOR AB NORRV�GEN 39 ARVIDSJAUR 0960-123 41 TOMMY HEDMAN
* 1900003 G�TEBORGS SKOG & TR�DG�RD AB DATAV�GEN 21 A ASKIM 031-68 38 80 BJ�RN HASSLING
* 1900004 TINGSRYDS MOTORTJ�NST L�V�NGEN PL 1068 TINGSRYD 0477-105 46 INGE SVENSSON
* 1900005 HILDINGS MOTOR INDUSTRIV�GEN 4 YTTERHOGDAL 0680-600 77 HILDING
* 1900006 KUNGSBACKA J�RN & FRITID AB TEKNIKGATAN 8 KUNGSBACKA 0300-188 00 MIKAEL DELLN�S
* 1900007 PSB MASKIN AB NORRBACKAGATAN 23 STOCKHOLM 08-33 78 80 PUTTE N�TBERG
* 1900008 S�G & TR�DG�RDSCENTER BOX 111 S�FFLE 0533-101 90 PER-�KE VARILENGEN



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>PHP test</title>
<meta http-equiv="content-type"
content="text/html; charset=utf-8/>
</head>
<?php
$db = mssql_connect('SERVERNAME', 'USER','PASSWORD');
if (!$db)

{
$output= 'Uppkopplingen gick inte bra.';
echo $output;
exit();
}
else
{
mssql_select_db('[DATABASE]',$db);
$output= 'Ansluten till Databasen.<br>';
echo $output;

}
echo 'Nu är jag ansluten till databasen... Skickar en fråga till SQL-databasen...<br>';
// Send a select query to MSSQL
$query = mssql_query('select top 10 No_, Name, Address, City, [Contact person], [Phone No_] from [STIHL SE$Customer]where Market=1');


// Check if there were any records
if (!mssql_num_rows($query)) {
echo 'Hittade inga poster';
} else
{
echo 'Antal funna rader: ' . mssql_num_rows($query) . '<br>';
// The following is equal to the code below:
//
// while ($row = mssql_fetch_row($query))


echo '<ul>';

while ($row = mssql_fetch_assoc($query)) {
echo '<li>' . $row['No_'] . ' '. $row['Name'] .' '. $row['Address'].' '. $row['City'] .' '. $row['Phone No_'] .' '. $row['Contact person'] .'</li>';
}

echo '</ul>';

}
// Free the query result
mssql_free_result($query);
?>

Any suggestions?

djr33
07-09-2010, 08:18 PM
Everything must be UTF8-- the database, your html pages, possibly your server configuration, etc. Go through and make sure everything is set to UTF8 and you should find the problem.

UTF8 is a setting for how a certain program will read information. If even one part is set to something else, then it will be translated into a weird format.

Chewbacca
07-14-2010, 06:48 PM
OK I understand, but still not...:confused:

Surely there must be some way of handling different character sets? I am not allowed to change the collation of the database which is set to 'SQL_Latin1_General_CP850_CS_AS'... And what character set in HTML/PHP corresponds to that ?

djr33
07-15-2010, 07:42 AM
That's probably ISO. You can serve your page as ISO, and because Swedish is not that weird (compared to characters like in Chinese or Russian), I think ISO will be ok. Then you will need to use the same format in your html, etc.
Also, there are functions in PHP that could convert it if you want to use unicode, such as utf8_encode().

Chewbacca
07-15-2010, 01:34 PM
Interesting... I've got a bunch of new stuff to try out. Thanks man!

djr33
07-15-2010, 05:51 PM
Sorry I don't have more specific advice, and I hope that works. It's always difficult to coordinate all the formats, but after you work this out it will always be correct (unless you change servers or something). So at least that's nice.

By the way, two ideas to help:
1. Do you have phpmyadmin or another web interface? I don't know what ms sql uses, but that's common for mysql. If you do, then you could look at what encoding those pages are being sent in and see if that helps at all.
2. If you google specific terms (encodings, database configurations, etc.) all in quotes (separately), then you might find someone else who had this same problem and solved it.