Special characters showing up on content retrieved from mysql using php?

My PHP connect code
have content stored in a mysql database. When I am trying to retrieve the content using PHP and display it using html and CSS, some of it displays these special characters ������.

  $dsn      = 'mysql:host=localhost;dbname=theme';

  $user     = 'root';

  $password = '';

  try {

    $con = new PDO($dsn, $user, $password);

    $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

  } catch (PDOException $e) {

    echo "Unable to connect" . " " . $e->getMessage();

  }

Add var options to set NAMES utf8.
Then add options to $con = new PDO();

  $dsn      = 'mysql:host=localhost;dbname=theme';

  $user     = 'root';

  $password = '';

  $options  = array(

    PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',

  );

  try {

    $con = new PDO($dsn, $user, $password, $options);

    $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

  } catch (PDOException $e) {

    echo "Unable to connect" . " " . $e->getMessage();

  }
1 Like

This works for me thanks