1

I am trying to get hindi data from database but it returns ???? everytime.

Other answers on StackOverflow told to use mysqli_set_charset($conn, 'utf8'); but this is not working in my case.

Please check the below code.

<?php
class Quotes {
  public $id;
  public $position;
  public $quote;
  public $image_url;

  private $conn;
  private $table_name;

  public function __construct($db) {
    $this->conn = $db;
    $this->table_name = "tbl_y_test";
  }

  //read all data
  public function getAllData() {
    $sql_query = "SELECT * FROM ".$this->table_name." ORDER BY `position`";
    mysqli_set_charset($conn, 'utf8');
    $obj = $this->conn->prepare($sql_query);

    $obj->execute();

    return $obj->get_result();
  }
}

?>
Ankush Kapoor
  • 365
  • 3
  • 13
  • Is the data in your DB correctly? What is charset of column you are retrieving? What is charset of page outputting on? – user3783243 May 24 '20 at 04:14

1 Answers1

0

You should set the character set of the data to UTF8. You can do it on database level:

    CREATE DATABASE db_name 
    DEFAULT CHARACTER SET UTF8;

and that way any tables created later will be in that character set by default.

You can set the character set only on table level (if you are going to store the Hindi data only in one specific table):

    CREATE TABLE tbl_y_test(
       ...
    ) ENGINE=InnoDB
    CHARACTER SET UTF8;

That way all text strings in that table will be UTF8 encoded.

Or you can even do it per-column (so only one column in specific table is UTF8 encoded):

    CREATE TABLE score(
       ...
       name VARCHAR(32) NOT NULL CHARACTER SET UTF8,
       ...
    ) ENGINE=InnoDB;
Philip Petrov
  • 975
  • 4
  • 8