0

I have this code:

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

if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

mysqli_set_charset($conn,'latin1_swedish_ci');

echo mysqli_character_set_name($conn);

$sql = "SELECT * FROM ggg";

$result = $conn->query($sql);

if ($result->num_rows > 0) {

  while($row = $result->fetch_assoc()) {

    echo "keyword: " . $row["keyword"]. " - Name: " . $row["firstname"]. " " . $row["lastname"] . $row["info"] . "<br>";
    
  }
} else {
  echo "0 results";
}

The $row["info"]-parts in the echoed result are full of questionmarks inside a rectangle instead of the swedish signs "å, ä, ö". Although the charset is correctly set to "latin1_swedish_ci" which is exactly the same as in the database (which I checked from phpMyAdmin).

What to try next, anyone have any ideas?

Rick James
  • 135,179
  • 13
  • 127
  • 222
ruja
  • 3
  • 2
  • You can use some of these answers here - https://stackoverflow.com/questions/10829816/set-character-set-using-mysqli – Zlatin Hristov Jun 25 '20 at 12:11
  • Yes - those answer exactly to use mysqli_set_charset - which I am using in my code. Please note that I'm also echoing the set charset - just to check that it is properly set, which it is. Despite this, the result is full of questionmark-symbols. – ruja Jun 25 '20 at 12:20
  • 1
    That's because the page is not support unicode at the first place. if you have a reason you can't convert database to some utf8 supported (like utf8_general_ci). On the other hand try to add utf8 header in php script at the begining (eg: header('Content-Type: text/html; charset=utf-8');) – Zlatin Hristov Jun 25 '20 at 12:49
  • You have your text in *some* encoding in the database, doesn't really matter which. You set your *connection encoding* to some encoding, here "latin1_swedish_ci" (which I'm not sure is really valid as an encoding, as it's a *collation*; just `latin1` should do). So you get text from your database in said connection encoding. You then send it to your browser. How the browser interprets it depends on whether you set explicit HTTP `Content-Type` headers and/or HTML meta tags. Somewhere along that chain something's out of line. – deceze Jun 25 '20 at 13:22
  • See "question mark" in https://stackoverflow.com/questions/38363566/trouble-with-utf8-characters-what-i-see-is-not-what-i-stored – Rick James Sep 04 '20 at 03:00

1 Answers1

1

Firstly you may want to use utf8 or utf8mb4 unless you have a reason not to.

My guess is what you are seeing is not really a problem that comes out of the database, but its from rendering in the browser which does not know how to display what you are providing.

You may verify if this is the case in chrome using an extension, or in other browsers in other ways.

Alternately, set the document headers in HTML so the browser dont have to guess :)

  • 1
    Well, it helped to change the whole database collation to utf-8 and then I think there was some problem when I tried to change charset - I used a too long code, and as there wasn't an error message, I thought everything was fine. I checked the code and used "utf8mb4" and then it started working. Phew, coding has such strange problems to it. – ruja Jun 25 '20 at 14:10