How to make MySql return utf-8?

5

When requesting information from my bank that contains special characters I get " " but in phpmyadmin in grouping is "UTF-8" and my site also has the UTF-8 goal what can I do to return the correct one?

    
asked by anonymous 15.08.2017 / 20:59

4 answers

4

One suggestion is to convert all input into the database into html entities, as follows the php code:

$email = addslashes(htmlentities($_POST['email'], ENT_QUOTES,'UTF-8')); 

In this way, all special characters are converted, and at the time of displaying them, the browser itself will interpret it.

It will be converted according to the table available at link

á = á (& aacute)

ã = ã (& atilde)

and so on

    
15.08.2017 / 21:48
6

I do not know if it's the ideal, but you can query it that way too

$sql = "SELECT * FROM tbl_produto WHERE nome_produto = :nome_produto collate utf8_unicode_ci ";
    
15.08.2017 / 21:03
4

If you are using PDO add this line:

$pdo -> exec("SET CHARACTER SET utf8");
    
15.08.2017 / 21:05
1

If you are using mysqli, use the mysqli_set_charset ($ connection, "utf8") function. This function defines the default character set to send and receive data to the Database. If you are using PDO and PHP above version 5.3.6 you can specify the character set in the DSN.

$dsn = sprintf("mysql:host=%s;dbname=%s;charset=utf8", $host, $dbname);
                $connection = new \PDO($dsn, $user, $password);

Or (For versions of PHP prior to 5.3.6)

$dsn = sprintf("mysql:host=%s;dbname=%s", $this->host, $this->dbname);
                    $connection = new \PDO($dsn, $this->user, $this->password);
$connection->exec('SET names = utf-8');
    
15.08.2017 / 21:18