Problems with danish char in database

Discussion in 'XML' started by Tine Müller, Apr 2, 2008.

  1. Tine Müller

    Tine Müller Guest

    Can someone help me what code to change so it's functioning with danish char
    which is in the database?

    <?php
    require("phpsqlsearch_dbinfo.php");

    // Get parameters from URL
    $center_lat = $_GET["lat"];
    $center_lng = $_GET["lng"];
    $radius = $_GET["radius"];

    // Start XML file, create parent node
    $dom = new DOMDocument("1.0");
    $node = $dom->createElement("markers_library_copy");
    $parnode = $dom->appendChild($node);

    // Opens a connection to a mySQL server
    $connection=mysql_connect (localhost, $username, $password);
    if (!$connection) {
    die("Not connected : " . mysql_error());
    }

    // Set the active mySQL database
    $db_selected = mysql_select_db($database, $connection);
    if (!$db_selected) {
    die ("Can\'t use db : " . mysql_error());
    }

    // Search the rows in the markers_library_copy table
    $query = sprintf("SELECT address, name, lat, lng, ( 3959 * acos( cos(
    radians('%s') ) * cos( radians( lat ) ) * cos( radians( lng ) -
    radians('%s') ) + sin( radians('%s') ) * sin( radians( lat ) ) ) ) AS
    distance FROM markers_library_copy HAVING distance < '%s' ORDER BY distance
    LIMIT 0 , 20",
    mysql_real_escape_string($center_lat),
    mysql_real_escape_string($center_lng),
    mysql_real_escape_string($center_lat),
    mysql_real_escape_string($radius));
    $result = mysql_query($query);

    if (!$result) {
    die("Invalid query: " . mysql_error());
    }

    header("Content-type: text/xml");

    // Iterate through the rows, adding XML nodes for each
    while ($row = @mysql_fetch_assoc($result)){
    $node = $dom->createElement("marker");
    $newnode = $parnode->appendChild($node);
    $newnode->setAttribute("name", $row['name']);
    $newnode->setAttribute("address", $row['address']);
    $newnode->setAttribute("lat", $row['lat']);
    $newnode->setAttribute("lng", $row['lng']);
    $newnode->setAttribute("distance", $row['distance']);
    }

    echo $dom->saveXML();

    ?>



    --
    M.v.h.
    Tine Müller

    homepage: http://tine_muller.homepage.dk/
     
    Tine Müller, Apr 2, 2008
    #1
    1. Advertising

  2. Tine M?ller wrote:

    > X-Newsreader: Microsoft Outlook Express 6.00.2900.3138


    Start with your newsreader^W Outlook Express.
    You need to choose

    Tools → Options → Send
    Mail Sending Format → Plain Text Settings → Message format MIME
    News Sending Format → Plain Text Settings → Message format MIME
    Encode text using: None

    in order to send special, non-ASCII characters.
     
    Andreï½ï½“ Prilï½ï½, Apr 2, 2008
    #2
    1. Advertising

  3. "Andreï½ï½“ Prilï½ï½" wrote
    > Start with your newsreader^W Outlook Express.
    > You need to choose
    >
    > Tools → Options → Send
    > Mail Sending Format → Plain Text Settings → Message format MIME
    > News Sending Format → Plain Text Settings → Message format MIME
    > Encode text using: None
    >
    > in order to send special, non-ASCII characters.


    Is it done right now?
     
    Tine Müller, Apr 2, 2008
    #3
  4. Tine Müller, Apr 2, 2008
    #4
  5. Tine Müller

    Andy Dingley Guest

    On 2 Apr, 14:05, "Tine Müller" <> wrote:
    > Can someone help me what code to change so it's functioning with danish char
    > which is in the database?


    I had a good answer, but the accursed Google Groups interface ate it
    8-(

    Lose the <meta> in your page. You're sending a HTTP-Header too (better
    idea anyway) and that will take precedence. As the page is claiming
    to be UTF-8, then you'd better send UTF-8 / Unicode characters in it,
    not ISO-8859 characters. If your DB is working in ISO-8859 already,
    then you might find it easier to change the page generation code to
    send the right HTTP header.

    Read up (Wikipedia is good) on Unicode, ISO-8859 and the distinctions
    between "character sets" (lists of squiggles that humans use) and
    "character encoding" (sequences of bytes that computers use to refer
    to these squiggles).

    You _must_ use a character set that includes the squiggle you need.
    Danish is easy (commonplace ISO-8859-1 suffices), Czech or Polish is
    harder. In general these days, go straight to Unicode. This works for
    all languages at once, ISO-8859 requires you to swap between
    ISO-8859-1 and ISO-8859-4 depending on which side of the Baltic you
    are.

    You then need an encoding that works for the character set you use and
    is configured so that your DB content matches your page (or gets
    translated) and your page's header matches your page's content. Use
    UTF-8 for Unicode, use the relevant ISO-8859-* for ISO-8859 (and you
    would have to swap between sides of the Baltic).


    Alternatively (a very bad idea, I only mention it to save a follow-up
    pinhead doing so), use plain old ASCII as an encoding and HTML numeric
    character entities or HTML entity references. You will find that
    numeric character entities are no easier to generate than UTF-8 (if
    your DB contains ISO-8859) and that entity references are a big chunk
    of tedious look-up code to create (maybe a PHP library exists for
    this). This approach is tedious, but it does work in cases where you
    can't fix the web server's HTTP-Headers to indicate a correct or
    useful encoding.
     
    Andy Dingley, Apr 2, 2008
    #5
  6. Tine Müller wrote:

    > MIME-Version: 1.0
    > Content-Type: text/plain;
    > charset="UTF-8";
    >
    > Is it done right now?


    Very well - yes.
     
    Andreï½ï½“ Prilï½ï½, Apr 4, 2008
    #6
  7. Tine Müller

    Tine Müller Guest

    "Andy Dingley" wrote
    I had a good answer, but the accursed Google Groups interface ate it
    8-(

    I'm not sure what you mean here?

    I have trouble understand your technic explanation and maybe it's because my
    language is danish and that this is NOT easy to understand, sorry.

    I have made some inprovements on another testmap
    http://www.tinemuller.dk/googlemap/tutorial/storelocator/library_phpsqlsearch_map.htm
    but there is still problems. If you look in the code you can see that I have
    tried a lot of doctype and charset. In my table on my host is says
    "utf8_danish_ci" but try search for "Smakkegårdsvej 112" and "1" and you
    will see the problem and then try with "3" and you will see an error message
    in Firebug.

    I have ask'ed Pamelas who made the tutorial here
    http://code.google.com/support/bin/answer.py?answer=87134&topic=11369&ctx=sibling
    to show us the code to use when you use foreign language as for instance
    danish but haven't heard anything from here yet.

    If you would help me some more I could send you my files to you privately
    and maybe you could test on your host also using "utf8_danish_ci" to see if
    YOU can get it to work, please?

    library_phpsqlsearch_genxml.php

    <!-- <!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="da" lang="da">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"
    />-->

    <!-- <!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="da" lang="da">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />-->

    <!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">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <title>Tine - Google Maps AJAX + MySQL/PHP Example</title>
    <script
    src="http://maps.google.com/maps?file=api&v=2&key=ABQIAAAAmYYoLeRyazVGDeFwMH_5chRBa9hFh1JQE70w75ZW5bLNFA531BSqFdAlOuedN5j5p0Nzmek2tUeZkA"
    type="text/javascript"></script>
    <script type="text/javascript">

    //<![CDATA[
    var map;
    var geocoder;

    var iconBlue=new GIcon();
    iconBlue.image='http://labs.google.com/ridefinder/images/mm_20_blue.png';
    iconBlue.shadow='http://labs.google.com/ridefinder/images/mm_20_shadow.png';
    iconBlue.iconSize=new GSize(12, 20);
    iconBlue.shadowSize=new GSize(22, 20);
    iconBlue.iconAnchor=new GPoint(6, 20);
    iconBlue.infoWindowAnchor=new GPoint(5, 1);

    var iconRed=new GIcon();
    iconRed.image='http://labs.google.com/ridefinder/images/mm_20_red.png';
    iconRed.shadow='http://labs.google.com/ridefinder/images/mm_20_shadow.png';
    iconRed.iconSize=new GSize(12, 20);
    iconRed.shadowSize=new GSize(22, 20);
    iconRed.iconAnchor=new GPoint(6, 20);
    iconRed.infoWindowAnchor=new GPoint(5, 1);

    var iconYellow=new GIcon();
    iconYellow.image='http://labs.google.com/ridefinder/images/mm_20_yellow.png';
    iconYellow.shadow='http://labs.google.com/ridefinder/images/mm_20_shadow.png';
    iconYellow.iconSize=new GSize(12, 20);
    iconYellow.shadowSize=new GSize(22, 20);
    iconYellow.iconAnchor=new GPoint(6, 20);
    iconYellow.infoWindowAnchor=new GPoint(5, 1);

    var customIcons = [];
    customIcons["SH"]=iconBlue;
    customIcons["SF"]=iconRed;
    customIcons["SB"]=iconYellow;

    customIcons["FH"]=iconBlue;
    customIcons["FF"]=iconRed;
    customIcons["FB"]=iconYellow;

    customIcons["JH"]=iconBlue;
    customIcons["JF"]=iconRed;
    customIcons["JB"]=iconYellow;

    customIcons["OH"]=iconBlue;
    customIcons["OF"]=iconRed;
    customIcons["OB"]=iconYellow;


    function load() {
    if (GBrowserIsCompatible()) {
    geocoder = new GClientGeocoder();
    map = new GMap2(document.getElementById('map'));
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    //map.setCenter(new GLatLng(40, -100), 4);
    map.setCenter(new GLatLng(56.26392, 9.501785), 6);
    }
    }

    function searchLocations() {
    var address = document.getElementById('addressInput').value;
    geocoder.getLatLng(address, function(latlng) {
    if (!latlng) {
    alert(address + ' not found');
    } else {
    searchLocationsNear(latlng);
    }
    });
    }

    function searchLocationsNear(center) {
    var radius = document.getElementById('radiusSelect').value;
    var searchUrl = 'library_phpsqlsearch_genxml.php?lat=' + center.lat() +
    '&lng=' + center.lng() + '&radius=' + radius; alert(searchUrl)
    GDownloadUrl(searchUrl, function(data) {
    var xml = GXml.parse(data);
    var markers = xml.documentElement.getElementsByTagName('marker');
    map.clearOverlays();

    var sidebar = document.getElementById('sidebar');
    sidebar.innerHTML = '';
    if (markers.length == 0) {
    sidebar.innerHTML = 'No results found.';
    //map.setCenter(new GLatLng(40, -100), 4);
    map.setCenter(new GLatLng(56.26392, 9.501785), 6);

    return;
    }

    var bounds = new GLatLngBounds();
    for (var i = 0; i < markers.length; i++) {
    var name = markers.getAttribute('name');
    var address = markers.getAttribute('address');
    var icon_type = markers.getAttribute('icon_type');
    var distance = parseFloat(markers.getAttribute('distance'));
    var point = new GLatLng(parseFloat(markers.getAttribute('lat')),
    parseFloat(markers.getAttribute('lng')));

    var marker = createMarker(point, name, address, icon_type);
    //var marker = createMarker(point, name, address);
    map.addOverlay(marker);
    var sidebarEntry = createSidebarEntry(marker, name, address,
    distance);
    sidebar.appendChild(sidebarEntry);
    bounds.extend(point);
    }
    map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
    });
    }

    function createMarker(point, name, address, icon_type) {
    var marker = new GMarker(point, customIcons[icon_type]);
    GLog.write(icon_type);
    //function createMarker(point, name, address) {
    //var marker = new GMarker(point);
    //GLog.write(icon_type);
    //var marker = new GMarker(point, {icon:customIcons[type]});


    var html = '<b>' + name + '</b> <br/>' + address;
    GEvent.addListener(marker, 'click', function() {
    marker.openInfoWindowHtml(html);
    });
    return marker;
    }

    function createSidebarEntry(marker, name, address, distance) {
    var div = document.createElement('div');
    var html = '<b>' + name + '</b> (' + distance.toFixed(1) + ')<br/>' +
    address;
    div.innerHTML = html;
    div.style.cursor = 'pointer';
    div.style.marginBottom = '5px';
    GEvent.addDomListener(div, 'click', function() {
    GEvent.trigger(marker, 'click');
    });
    GEvent.addDomListener(div, 'mouseover', function() {
    div.style.backgroundColor = '#eee';
    });
    GEvent.addDomListener(div, 'mouseout', function() {
    div.style.backgroundColor = '#fff';
    });
    return div;
    }
    //]]>
    </script>
    </head>

    <body onload="load()" onunload="GUnload()">
    Address: <input type="text" id="addressInput"/>

    Radius: <select id="radiusSelect">
    <option value="1" selected="selected">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    <option value="25">25</option>
    <option value="100">100</option>

    <option value="200">200</option>
    </select>
    <input type="button" onclick="searchLocations()" value="Search
    Locations"/>
    <br/>
    <br/>
    <div style="width:600px; font-family:Arial,
    sans-serif; font-size:11px; border:1px solid black">
    <table>
    <tbody>
    <tr id="cm_mapTR">
    <td width="200" valign="top"> <div id="sidebar" style="overflow:
    auto; height: 400px; font-size: 11px; color: #000"></div>

    </td>
    <!-- <td> <div id="map" style="overflow: hidden; width:400px;
    height:400px"></div> </td> -->
    <td> <div id="map" style="border: 1px solid black; overflow: hidden;
    width:400px; height:400px"></div> </td>
    </tr>
    </tbody>
    </table>
    </div>
    </body>
    </html>

    /Tine
     
    Tine Müller, Apr 5, 2008
    #7
  8. Tine Müller

    Tine Müller Guest

    It should have been:

    library_phpsqlsearch_genxml.php

    "<?php
    require("phpsqlsearch_dbinfo.php");

    // Get parameters from URL
    $center_lat = $_GET["lat"];
    $center_lng = $_GET["lng"];
    $radius = $_GET["radius"];

    // Start XML file, create parent node
    $dom = new DOMDocument("1.0");
    $node = $dom->createElement("markers_library");
    $parnode = $dom->appendChild($node);

    // Opens a connection to a mySQL server
    $connection=mysql_connect (localhost, $username, $password);
    if (!$connection) {
    die("Not connected : " . mysql_error());
    }

    // Set the active mySQL database
    $db_selected = mysql_select_db($database, $connection);
    if (!$db_selected) {
    die ("Can\'t use db : " . mysql_error());
    }

    // Search the rows in the markers_library table
    $query = sprintf("SELECT address, name, lat, lng, icon_type, ( 6371 *
    acos( cos( radians('%s') ) * cos( radians( lat ) ) * cos( radians( lng ) -
    radians('%s') ) + sin( radians('%s') ) * sin( radians( lat ) ) ) ) AS
    distance FROM markers_library HAVING distance < '%s' ORDER BY distance LIMIT
    0 , 20",
    mysql_real_escape_string($center_lat),
    mysql_real_escape_string($center_lng),
    mysql_real_escape_string($center_lat),
    mysql_real_escape_string($radius));
    $result = mysql_query($query);

    if (!$result) {
    die("Invalid query: " . mysql_error());
    }

    header("Content-type: text/xml");

    // Iterate through the rows, adding XML nodes for each
    while ($row = @mysql_fetch_assoc($result)){
    $node = $dom->createElement("marker");
    $newnode = $parnode->appendChild($node);
    $newnode->setAttribute("name", $row['name']);
    $newnode->setAttribute("address", $row['address']);
    $newnode->setAttribute("lat", $row['lat']);
    $newnode->setAttribute("lng", $row['lng']);
    $newnode->setAttribute("distance", $row['distance']);
    $newnode->setAttribute("icon_type", $row['icon_type']);
    }

    echo $dom->saveXML();
    ?>
     
    Tine Müller, Apr 5, 2008
    #8
  9. Tine Müller

    Tine Müller Guest

    I have made some improvements. Now all the markers can be find. The only
    problem now is making æ ø å instead of an ?.

    I changed:
    // Start XML file, create parent node
    //$dom = new DOMDocument("1.0");
    $dom = new DOMDocument('1.0', 'utf-8');

    OS: Debian Linux 4.0 php 5.2

    /Tine
     
    Tine Müller, Apr 6, 2008
    #9
  10. Tine Müller

    Tine Müller Guest

    "Tine Müller" wrote
    >I have made some improvements. Now all the markers can be find. The only
    >problem now is making æ ø å instead of an ?.
    >
    > I changed:
    > // Start XML file, create parent node
    > //$dom = new DOMDocument("1.0");
    > $dom = new DOMDocument('1.0', 'utf-8');
    >
    > OS: Debian Linux 4.0 php 5.2


    In Explorer 6 NOTHING is functioning. One minutes from going crazy.:-(
     
    Tine Müller, Apr 6, 2008
    #10
  11. Tine Müller wrote:
    > I have made some improvements. Now all the markers can be find. The only
    > problem now is making æ ø å instead of an ?.
    >
    > I changed:
    > // Start XML file, create parent node
    > //$dom = new DOMDocument("1.0");
    > $dom = new DOMDocument('1.0', 'utf-8');


    Have you followed this advice
    "Note: DOM extension uses UTF-8 encoding. Use utf8_encode() and
    utf8_decode() to work with texts in ISO-8859-1 encoding or Iconv for
    other encodings."
    in the documentation <URL:http://www.php.net/manual/en/ref.dom.php>?


    That means for instance you need to do

    header('Content-Type: application/xml');
    $doc = new DOMDocument();
    $doc->appendChild($doc->createElement('root'));
    $doc->documentElement->appendChild($doc->createTextNode(
    utf8_encode('æ ø å')));
    echo $doc->saveXML();

    So unless your data from the database comes out as UTF-8 you need to
    make sure you encode it first before passing it to any DOM methods like
    createTextNode or setAttribute.


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Apr 6, 2008
    #11
  12. Tine Müller

    Tine Müller Guest

    Thanks for trying to help but it will not work no matter what I do.:-(

    I would ask you if you would please tell me WHAT code to change from the
    tutorial
    http://code.google.com/support/bin/answer.py?answer=87134&topic=11364 when
    it used with danish char in the table so that I can try that? And I will use
    utf-8 all the places and then it should work but it doesn't.:-(

    Information about webserver
    OS: Debian Linux 4.0
    Webserver: Apache 2
    PHP: Yes, PHP 5.2 - phpinfo
    PHP safe-mode: Not on
    MySQL: Yes, MySQL 5.0

    My table use "utf8_danish_ci".

    /Tine
     
    Tine Müller, Apr 7, 2008
    #12
  13. Tine Müller

    Tine Müller Guest

    Just forget this last post. A danish guy gave me the "easy" tip.

    $name=$row['name'];
    $name=utf8_encode($name);
    $newnode->setAttribute("name", $name);
    $address=$row['address'];
    $address=utf8_encode($address);
    $newnode->setAttribute("address", $address);

    Everything is now functioning
    http://www.tinemuller.dk/googlemap/tutorial/storelocator/library_phpsqlsearch_map.htm

    /Tine



    "Tine Müller" <> skrev i en meddelelse
    news:GckKj.3$2net.dk...
    > Thanks for trying to help but it will not work no matter what I do.:-(
    >
    > I would ask you if you would please tell me WHAT code to change from the
    > tutorial
    > http://code.google.com/support/bin/answer.py?answer=87134&topic=11364 when
    > it used with danish char in the table so that I can try that? And I will
    > use utf-8 all the places and then it should work but it doesn't.:-(
    >
    > Information about webserver
    > OS: Debian Linux 4.0
    > Webserver: Apache 2
    > PHP: Yes, PHP 5.2 - phpinfo
    > PHP safe-mode: Not on
    > MySQL: Yes, MySQL 5.0
    >
    > My table use "utf8_danish_ci".
    >
    > /Tine
    >
    >
    >
    >
    >
    >
     
    Tine Müller, Apr 7, 2008
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Klaus Jensen

    Danish chars not displaying

    Klaus Jensen, Dec 11, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    416
    Klaus Jensen
    Dec 11, 2003
  2. =?Utf-8?B?TmFtZXNwYWNl?=

    Problem with cookie and htmencoding (danish characters)

    =?Utf-8?B?TmFtZXNwYWNl?=, Jul 18, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    468
    =?Utf-8?B?TmFtZXNwYWNl?=
    Jul 18, 2005
  3. LarsM
    Replies:
    18
    Views:
    1,173
    Andreas Prilop
    Feb 11, 2005
  4. Jens Jensen

    Strange danish letters bug.

    Jens Jensen, Feb 13, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    356
    Jens Jensen
    Feb 13, 2007
  5. lovecreatesbeauty
    Replies:
    1
    Views:
    1,096
    Ian Collins
    May 9, 2006
Loading...

Share This Page