2

I've such a problem , first take a look at mysql table =>

CREATE TABLE IF NOT EXISTS users(
id int(4) NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(40) CHARSET utf8 COLLATE 'utf8_unicode_ci' NOT NULL,
surname VARCHAR(40) CHARSET utf8 COLLATE 'utf8_unicode_ci' NOT NULL,
);

It is storing data successfully , but when I'm trying to retrieve this info to my .php file (which encoding is also utf8) it still showing me question marks (?????), why ? How can I solve it ?

UPDATE

Something I'm not doing well. So I've 2 php files, one is classA.php file in which I've defined class which is retrieving info from database and I've included this file (classA.php) into my default.php file where I want to see data. I've exactly same table which is written above , and I'm writing

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

in the first line in default.php, but it still doesn't work, thanks for advices :))

SECOND UPDATE

This script I've in classA.php file , and its encoding is default like default.php file encoding. I just added in default.php file in first line this

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

but it still doesn't work.

Third update

sql =>

create table ok(
id int(2) not null auto_increment primary key,
name varchar(20) charset utf8 not null);

and php file

<?php
 header('Content-Type: text/html; charset=UTF-8');
 ?>
 <!DOCTYPE html>
 <html>
 <head>
        <title>hello</title>
          <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    </head>
     <body>


     <?php
        $con = mysqli_connect("host","user","pass","db");

       if (mysqli_connect_errno()==0){
       if ($r = mysqli_query($con,"SELECT * FROM ok")){
    mysqli_set_charset($con,"utf8");
    while ($d = mysqli_fetch_assoc($r)){
        echo $d['name'] . "<br>";
           }
           }
          }

            if (isset($con)){
               mysqli_close($con);
             }
             ?>


               </body>
               </html>

I've inserted in ok table this=>

insert into ok(name) values("one"),("ერთი"),("two"),("ორი");

PS. special characters are Georgian :)

and it results English characters fine and Georgians with question marks :(

It doesn't work anyways :(

5
  • how are you retrieving it and how are you "encoding is utf8"? Commented Jul 13, 2012 at 9:40
  • I'm retrieving it with php script, want to post this script too ? .php file encoding is utf8 too , when I'm typing foreign letters it is saving and showing , but when I am trying to retrieve from database it shows me quesiton marks Commented Jul 13, 2012 at 9:43
  • do you: SET CHARACTER SET utf8 SET NAMES utf8 ini_set( "default_charset", "UTF-8"); Commented Jul 13, 2012 at 9:45
  • no , in php.ini it is default: latin1 Commented Jul 13, 2012 at 9:48
  • look at the answers by Nadav S. and Mihai Stancu Commented Jul 13, 2012 at 9:49

3 Answers 3

7

The fact that the PHP file is in UTF-8 doesn't necessarily mean that the data coming from/ going to the database is in UTF-8 too.

You didn't mention which extension you're using, but:

  • For mysql use mysql_set_charset($link,'utf8');

  • For mysqli use mysqli->set_charset('utf8') or the same as above with mysqli_

  • For PDO, when you connect include charset:utf8 in the DSN string.

Sign up to request clarification or add additional context in comments.

12 Comments

Not coming as UTF8 does not prevent transfer as UTF8. It prevents you from doing proper utf8 multi-byte operations on the string.
I agree that charset:utf8 should be used because it will solve other problems that may arise later-on when processing the data in PHP before sending it to the client/ browser. But the issue here is the fact that the client/ browser does not know what encoding should be used.
@MihaiStancu interesting. Let's wait for the questioner.
I'm using mysqli extension , so is it necessary to define mysqli->set_charset('utf8') in php script ?
@Tornike - Set the charset directly after creating the database connection (before you make a query). You can insert the UTF-8 encoded string directly in a UTF-8 encoded PHP page without converting it to HTML entities (&#4304;).
|
3

Declare the HTML utf8 encoding:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

And/ Or transfer encodings in the headers:

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

6 Comments

The file may be saved from his text editor as UTF8, that does not mean that the transfer encoding is UTF8
this header should be in the .php file where is written data retrieving php script ?
The header should be in the PHP that returns the data to the browser.
It's OK to have latin1 encoding in your PHP files. And it should work just fine as long as you don't use multi-byte characters within the source code (and even then it should work it would just display them wrong in your text editor).
The BIG issue is not to have some files saved as UTF8, and others as latin1, and especially not to use Unicode UTF8 with BOM. Because BOM especially and different encodings particularly may yield some characters when including each other. That means that including a Class file may echo the BOM/ Other missinterpreted encoding and thus make you unable to use redirects because that character was sent to the browser and you get the classic "headers already sent" error. Debugging is a dread in that case because you don't know which file was saved with different encoding/ where the inclusion is.
|
1

try with

htmlentities($row['name'],ENT_QUOTES);

see htmlentities

1 Comment

That does not fix the problem, it sweeps it under the rug. But if your special characters are not outside of the defined entities spectrum it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.