0

I have struggled with this some time. I could not get it to work.

On the url http://course.easec.se/problem.pdf I have put together what I have done so far!

Any suggestions?

<?php 
header('Content-Type: text/html; charset=ISO-8859-1');  //no changes
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "db_test4";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
mysqli_set_charset("utf8");
echo "From PHP Script  ååååå\n";   //to see if there is problem when edit the script
$sql = "SELECT * FROM test_tbl";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {

        echo "Kundid: " . $row["kund_id"]. " - Förnamn: " . $row["fnamn"]. " - Efternamn: " . $row["enamn"]. " - Adress:" . $row["adress1"] ."<br>";
    }
} else {
    echo "0 results";
}
$conn->close();   
?>

Client:

Client

It works now, seems do be my editor, when I tried ANSI instead of UTF-8 and Changes the header to !

enter image description here

  • 2
    This is not an acceptable question. Please format it and include all relevant information directly in the question. That PDF can (and probably will) break at some point. – Tim Biegeleisen Oct 09 '16 at 11:18
  • if you're getting/outputting UTF from the database, why are you telling the browser to expect iso8859 text? "I'm going to be sending you a banana" and then throw a watermelon . – Marc B Oct 13 '16 at 20:46

3 Answers3

3

Directly below

$conn = new mysqli($servername, $username, $password, $dbname);

add

$conn->set_charset("utf8");

Note: In addition you should also make sure that the charset of your HTML (in the <head> tag) is set to utf-8, like <meta charset="utf-8">

Johannes
  • 64,305
  • 18
  • 73
  • 130
  • almost, that should be `utf8mb4` – Martin Oct 13 '16 at 20:51
  • @Martin I don't think so - the basic letters of languages like Swedish, German etc. (at least all Western European ones) are all included in utf8, and that's what the OP needs – Johannes Oct 13 '16 at 20:54
  • It's more through luck that OPs content would still work in this case, but `utf8` shouldn't be encouraged as it's only 3bits. why offer a third of a character set when the full character set can be offered? – Martin Oct 13 '16 at 20:55
  • if emojis are involved, yes, but if it's just "real language", it doesn't matter - utf8 will work. But of course: Why not offer more and use the same little part of it. – Johannes Oct 13 '16 at 21:04
  • You mean emojis such as the `£` Stirling symbol or the `€` euro symbol or the `-` dash symbol when entered into UTF-8 HTML page inputs and saved to the Db etc. etc. – Martin Oct 13 '16 at 21:26
  • @Martin No, these are part uf utf8, otherwise my websites wouldn't work properly. Just because they aren't on every keyboard, it doesn't mean they aren't included in utf8 – Johannes Oct 13 '16 at 22:33
  • it's nothing to do with keyboard, these characters get saved and output in `utf8_` charset mysql as two jumbled other characters. I've seen this numerous times, it's not a maybe. Cheers. – Martin Oct 14 '16 at 09:29
0

Use: header('Content-Type: text/html; charset=ISO-8859-1');

Instead of: header('Content-type: text/html; charset=utf-8');

No need to use inside while loop, use top of the page before print anything.

AHJeebon
  • 1,218
  • 1
  • 12
  • 17
  • No changes, sorry! /Mats – Mats Johannesson Oct 09 '16 at 12:36
  • Will you try again by removing `mysqli_set_charset("utf8");` if not work then again try with `mysqli_set_charset( $conn, 'ISO-8859-1');` – AHJeebon Oct 09 '16 at 12:50
  • It was the same problem, I found this article http://stackoverflow.com/questions/11120774/utf-8-with-mysql-and-php-in-freebsd-swedish-chars-%c3%a5%c3%a4%c3%b6 – Mats Johannesson Oct 13 '16 at 20:08
  • And I did all the steps, but no luck, then as a last effort I change editor to ANSI instead of UTF-8 then it works! I have a site on Azure and work with notepad as a editor, I Always use UTF-8. But your suggestion work, if you use UTF-8 it does not! – Mats Johannesson Oct 13 '16 at 20:13
0

I like how you set your question in a PDF, somewhat original :-D . Now, the answer to your problem will be found here. Read the whole question and as many of the answers as you can.

then as a last effort I change editor to ANSI instead of UTF-8 then it works!

Due to this the issue is your internal PHP script encoding so Reaseach using PHP Multibyte String and setting your PHP code to :

<?php
mb_internal_encoding('UTF-8'); //this is the important one! 
mb_http_output('UTF-8');
mb_http_input('UTF-8');

to ensure your PHP code is also UTF-8 complient when executed. You can also (or should be able to) edit the settings in your IDE editor to save PHP files without UTF-8 BOM (Byte Order Mark) which will also solve this problem and save them as UTF-8 understandable by other services.


Additional Notes (cos I like to moan about MySQL UTF-8):

MySQL UTF-8 is NOT the full UTF-8 . Solutions are offered in the linked thread above, some general additions:

  • 1) You want to always use UTF8mb4 (and ideally _unicode_ci).

  • 2) In the final screenshot of your PDF you should to set as many of these as practical to utf8mb4_unicode_ci.

  • 3) Setting the connection character set is vital, even if the server and the client both use utf8mb4 if the connection is only utf-8 it brings everything else down to that low point.

  • 4) Recommended:

    header('Content-Type: text/html; charset=UTF-8');  //UTF-8
    

Only MySQL UTF-8 is broken, other implementatios of it is not and is perfectly usable as and is generally regarded as an HTML standard.

Community
  • 1
  • 1
Martin
  • 22,212
  • 11
  • 70
  • 132