Transform rare characters in mysql [duplicated]

0

I'm collecting data stored in database, and I've seen that there are some columns that have strange characters.

Example (Extracted from php)

  

The sport is not being valued enough and the situation is very   fucked

Example (Stored in mysql)

  

The sport is not being valued enough and the situation is very   fucked

Code Excerpt

The object of type DatabaseConnection contains an instance of an object of type mysqli.

class DatabaseConnection {

  /**
   * @var mysqli
   **/
  protected $_db;

  /**
   * Database connection construct
   * @param   string  $host
   * @param   string  $user
   * @param   string  $password
   * @param   string  $bd
   * @construct
   **/
  public function __construct( $host = 'localhost', $user = 'root', $password = '***', $bd = 'testing' ){
    $this->_db = new mysqli( $host, $user, $password, $bd );

    if ( mysqli_connect_errno() ){
      return false;
    }

    return true;
  }

  /**
   * Do mysql query
   * @param string  $str
   **/
  public function query ( $str ){
    $res = $this->_db->query ( $str );

    return $res;
  }

  /**
   * Close mysql connection
   **/
  public function close(){
    $this->_db->close();
  }

}


$connection = new DatabaseConnection( $config['database']['host'], $config['database']['user'], $config['database']['pass'], $config['database']['db'] );

$query = "select * from news where tipo = 'news' limit 10";

$res = $connection->query( $query );
$arr = [];

if ( $res ){
  while ( $row = $res->fetch_assoc() ){
    $arr[] = $row;
  }

  return $arr;
}

I would like to know if there is any way of replacing these characters with their real value, in this case they would be vowels with accents.

Thank you,

Ismael.

    
asked by Ismael Moral 10.01.2017 в 18:18
source

1 answer

0

Use utf8_encode () to display the strings. link

I think if you use it in the whole array it works just the same.

$arr = utf8_encode($arr);

If not, you can do a function that traverses the array and replaces each string

    
answered by 10.01.2017 / 19:48
source