php unicode UTF-8 result in required files

2019-09-02 04:13发布

问题:

i add this define() function in my config.php file.

$options = DataAccess::FETCHLOAD("SELECT sign FROM " . OPTIONS . " WHERE 1");
define('_SIGN_',$options['0']['sign']);

Now, i required config.php file into my index.php page like this :

require $abspath .'/config.php';

  <!DOCTYPE html>
  <html>
  <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  </head>
  <body>
  <?PHP echo _SIGN_; ?>
  </body>
  </html>

now in output i have this result:

?????????????????????

worked result :

  <!DOCTYPE html>
  <html>
  <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  </head>
  <body>
  $options = DataAccess::FETCHLOAD("SELECT sign FROM " . OPTIONS . " WHERE 1");
  define('_SIGN_',$options['0']['sign']);
  <?PHP echo _SIGN_; ?>
  </body>
  </html>

output result :

تواصل معنا 

how do fix this for show unicode UTF-8 when i required config.php ?!

回答1:

Did you try after establishing connection to the database execute:

set_charset ( "utf8")

I use MySQLi and make it this way:

//-> conncecting to db
$mysqli = new mysqli ( $hostname, $username, $password, $db );

if ( $mysqli -> connect_errno ) {

    echo 'Failed to connect to MySQL: ' . $mysqli -> connect_error;

}

// change character set to utf8
if ( ! $mysqli -> set_charset ( "utf8") ) {

    printf ( "Error loading character set utf8: %s\n", $mysqli->error );
}


回答2:

Fist where did you set the variable $abspath ?

Then you have to put <?php and ?> tags around your require statemet and in your config file around your php!

Now the example below works fine for me and is pretty much the same as yours!

config.php:

<?php

    $options['0']['sign'] = "تواصل معنا";  //$options = DataAccess::FETCHLOAD("SELECT sign FROM " . OPTIONS . " WHERE 1");
    define('_SIGN_', $options['0']['sign']);


?>

index.php:

<?php require 'config.php'; ?>

  <!DOCTYPE html>
  <html>
  <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  </head>
  <body>
  <?PHP echo _SIGN_; ?>
  </body>
  </html>

And the Output is:

تواصل معنا