Include UTF-8 charset in mysqli connection

Asked

Viewed 12,072 times

9

Hello, after noticing that my code is full of strange characters like ??? I tried to fix it but found I need to change the charset through the mysqli connection, but the problem is I don’t know how to do it!
Can someone help me?

Here’s my connection code mysqli

config.php

<?php

//site specific configuration declartion
define( 'BASE_PATH', 'http://localhost/user_login/');
define( 'DB_HOST', 'localhost' );
define( 'DB_USERNAME', 'root');
define( 'DB_PASSWORD', '');
define( 'DB_NAME', 'login');

function __autoload($class)
{
    $parts = explode('_', $class);
    $path = implode(DIRECTORY_SEPARATOR,$parts);
    require_once $path . '.php';
}

?>

Dbclass

<?php

    class Cl_DBclass
    {
    /**
     * @var $con vai realizar conexão com o banco
     */
    public $con;

    /**
     * Isto irá criar uma conexão de banco de dados
     */
    public function __construct()
    {
        $this->con = mysqli_connect(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);
        if( mysqli_connect_error()) echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    }
?>

From now on Thank you!

  • What is the meeting of the bank and its page?

4 answers

9


After connecting to the bank, call set_charset, to change the charset. Use character_set_name() to know the current charset.

$this->con = mysqli_connect(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);
$this->con->set_charset("utf8");
  • This also did not solve :-(

6

Use this way with mysql:

You can change your constructor to:

public function __construct()
{
    $this->con = mysqli_connect(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);
    if(mysqli_connect_error()) { // < evite IF sem chaves
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    } else {
        mysqli_query("SET NAMES 'utf8'");
        mysqli_query('SET character_set_connection=utf8');
        mysqli_query('SET character_set_client=utf8');
        mysqli_query('SET character_set_results=utf8');
    }
}

Returns all queries smoothly in the encoding.

  • mysqli != mysql_*

  • @rray I know it’s different, that’s why I said I use it with mysql. I never used mysqli to know if it works. It can test.

  • 2

    The problem is that mysql_* is an obsolete API, it does not receive updates since ...

  • I put this code but continue the.

  • Thank you. My problem has been solved, but I note that mysqli_query() receives two parameters, the first being the connection!

4

In my connection, I put the charset below outside of php:

<meta charset="utf-8">

Inside php I put so:

$con->set_charset("utf8");

My pages html with "charset utf-8" and in the database collate "utf8_general_ci". Okay, the problem is solved.

1

Okay, Problem Solved! At first, the solutions given here were not working because I had included this code in an earlier attempt to fix:

<?php
    header('Content-Type: text/html; charset=UTF-8');
?>

Then I finished one of the solutions given, (both work) excludes it and changed the value of the meta tag charset of utf-8 for iso-8859-1 ! Thank you all!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.