0

I have one php file when I get data from MySQL where I set encoding to utf8. When Im tring to get data and display it in my php file I see there is sth wrong with some characters, e.g Magia ĹwiÄt w JaseĹkach 2013-12-23. I create all php files using utf8 encoding, but when I want to open this file from server Notepad++ showes me that there is ANSI as utf8 encoding and newline format is Macintosh.

Connecting to db

<?php
$db = new mysqli('############');
$db->query("SET NAMES utf8");
$db->query("SET CHARACTER SET utf8");
  if($db->connect_error) {
      $msg = "Cant connect";
  } else {
    $msg = "Works!";
  }
?>



<?php
require '../bootstrap/bootstrap.php';
$query = $db->real_escape_string("Select id, data_dodania,tytul, zajawka From artykuly ORDER BY data_dodania DESC");
$result = $db->query($query);
$result=$result->fetch_all();
    foreach($result as $row ) {
        echo '<tr>';
        echo '<td>';
          echo '<b>';
            echo $row[2];
            echo "<span class='text-info'> <small class='text-right'>$row[1] </small></span>";
          echo '</b>';
          echo '<br />';
          echo $row[3]; 
        echo '</td>';      

        echo '</tr>';

    } 
   echo '</table>';
pawel__86
  • 177
  • 1
  • 1
  • 11
  • 1
    How are you connecting to the database? – Burhan Khalid Dec 23 '13 at 13:33
  • Also, don't escape complete queries, just the values you want to use in them (although in this specific case it will not make a difference). Or better yet, use prepared statements. – jeroen Dec 23 '13 at 13:35
  • 3
    http://stackoverflow.com/questions/279170/utf-8-all-the-way-through – Mihai Dec 23 '13 at 13:37
  • I've added code to connect mysq – pawel__86 Dec 23 '13 at 13:39
  • Regarding utf8 usage: Are you setting both your notepad++ to edit your programs (not data) using utf8 as the encoding, AND setting the tables and fields (and possibly defaults on tables and fields and database) on MySQL so that your data is being saved in utf8? – Elliptical view Dec 23 '13 at 13:49

1 Answers1

0

The encoding you use on your Webpage and the encoding you have specified in the database setup. If the database connection use UTF-8 you probably should use UTF-8. There is no ANSI as UTF-8. Either you have UTF-8 or ANSI encoding.

In Notepad++ you can encode your file to UTF-8 if you go to Encoding -> Convert to UTf-8 Set this HTML-Tag to tell the browser, which encoding the browser should use for page display: <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

In PHP to convert an ISO-8859-1 string, use string utf8-encode(string $data) See here: UTF8-Encode

Sebi2020
  • 1,966
  • 1
  • 23
  • 40