Write MYSQL data in pt-br / UTF8 format

0

How do I save data in the database in Brazilian format with accents, I used this in connection

mysqli_set_charset($conexão, 'utf8');

But now my code is all in PDO how do I save this data in utf8 in PHPMyadmin database?

<?php

//Pagina de Conexão com o banco de dados

//Define as Variaveis PDO para conexão
define( 'DB_HOST', 'localhost' ); //Local de acesso do banco
define( 'DB_NAME', 'ifsp' ); //Nome do banco de dados
define( 'DB_USER', 'root' ); //Usuario do banco
define( 'DB_PASS', '' ); //Senha do banco de dados

// Tentar conexao
try 
{
    $pdo = new PDO( 'mysql:host=' . DB_HOST . ';dbname=' . DB_NAME, DB_USER, DB_PASS );

    //Se erro ativa o catch 
} catch ( PDOException $e ) 
{
    //Exibe o erro 
    echo "Erro: " . $e->getMessage() . "<br/>";
    
asked by anonymous 15.10.2015 / 01:45

4 answers

1

To define the character set in PDO you must specify the DSN with the remaining parameters of the connection.

...
try 
{
    $pdo = new PDO( 'mysql:host=' . DB_HOST . ';dbname=' . DB_NAME . '; charset=utf8;', DB_USER, DB_PASS );

    //Se erro ativa o catch 
} catch ( PDOException $e ) 
{
...

Or by specifying in PDO options.

...
$opc= array(
    PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
); 
$dsn = 'mysql:host=' . DB_HOST . ';dbname=' . DB_NAME;
$dbh = new PDO($dsn, $username, DB_USER, DB_PASS, $opc);
...

PDO MySQL DSN - PHP.net

    
15.10.2015 / 02:06
3

There are two ways:

  • by DNS;
  • by command SET NAMES (which can be executed at startup)

Ideally, you should use both because before PHP 5.3.6 the charset of DNS parameter was ignored.

<?php
$dsn = "mysql:host={$host};dbname={$database};charset=utf8"; // PHP >= 5.3.6
$options = array(
    PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8', // PHP < 5.3.6
); 
$conexao = new PDO($dsn, $user, $pass, $options);

I'd rather use a condition to execute the command only when needed:

<?php
$dsn = "mysql:host={$host};dbname={$database};charset=utf8";
$conexao = new PDO($dsn, $user, $pass);
if (version_compare(phpversion(), '5.3.6', '<')) {
    $conexao->exec('SET names utf8;');
}
    
16.10.2015 / 13:37
0

Try this:

$pdo = new PDO( 'mysql:host=' . DB_HOST . ';dbname=' . DB_NAME, DB_USER, DB_PASS.':charset=utf8' );

or

$pdo = new PDO("mysql:host=$host;dbname=$db", $user, $pass, 
               array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
                     PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
    
15.10.2015 / 01:58
0

Of all of you it worked very well Thanks

    <?php

//Pagina de Conexão com o banco de dados

//Define as Variaveis PDO para conexão
define( 'DB_HOST', 'localhost' ); //Local de acesso do banco
define( 'DB_NAME', 'ifsp' ); //Nome do banco de dados
define( 'DB_USER', 'root' ); //Usuario do banco
define( 'DB_PASS', '' ); //Senha do banco de dados

// Tentar conexao
try 
{
    $options= array(
        PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
    ); 
    $dsn = 'mysql:host=' . DB_HOST . ';dbname=' . DB_NAME;
    $pdo = new PDO($dsn, DB_USER, DB_PASS, $options);

    //Se erro ativa o catch 
} catch ( PDOException $e ) 
{
    //Exibe o erro 
    echo "Erro: " . $e->getMessage() . "<br/>";
}
    
16.10.2015 / 20:28