UTF 8 encoding problem

Discussion in 'PHP' started by Matt18, Feb 7, 2012.

  1. #1
    Hi

    I have a problem with encoding data I get from my database to UTF8.

    I have very limited knowladge of PHP.

    Can you please help me?

    Thank you in advance!


    <?php  
    require("db.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");
    $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 table
    $query = sprintf("SELECT address, name, lat, lng, ( 6371 * acos( cos( radians('%s') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( lat ) ) ) ) AS distance FROM markers 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();
    ?>
    
    
    Code (markup):
     
    Matt18, Feb 7, 2012 IP
  2. MarPlo

    MarPlo Member

    Messages:
    97
    Likes Received:
    2
    Best Answers:
    2
    Trophy Points:
    48
    #2
    Hi,
    Try replace:
    header("Content-type: text/xml");
    With:
    header('Content-type: text/xml; charset=utf-8');
    Code (markup):
    And the file to be saved with utf-8 unicode.
     
    MarPlo, Feb 7, 2012 IP
  3. ezprint2008

    ezprint2008 Well-Known Member

    Messages:
    611
    Likes Received:
    15
    Best Answers:
    2
    Trophy Points:
    140
    Digital Goods:
    1
    #3
    Trying to hack into Sprints GPS? lol :D wow
     
    ezprint2008, Feb 7, 2012 IP
  4. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #4
    after making a connection to your database run this query

    use SET NAMES utf8

    and then insert the data, then the data you read from the database will be utf8 :)
     
    EricBruggema, Feb 7, 2012 IP
  5. Matt18

    Matt18 Guest

    Messages:
    591
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    That's like extra strange. When I save file as UTF 8 instead of ANSI, it doesn't work anymore. When I save it back to ANSI, it works again... Any ideas?

    Just setting header to header('Content-type: text/xml; charset=utf-8'); doesn't work.

    All data in the db are properly encoded.

    Thank you all for your time. Please help me solve this problem
     
    Matt18, Feb 9, 2012 IP
  6. JohnnySchultz

    JohnnySchultz Peon

    Messages:
    277
    Likes Received:
    4
    Best Answers:
    7
    Trophy Points:
    0
    #6
    this doesn't help..
     
    JohnnySchultz, Feb 9, 2012 IP