Search code examples
phpmysqliprepared-statementspecial-characters

phph mysqli Retrieve special characters from database when using prepared statements


I am converting a php script to use prepared statements. But when I grab a piece of text from the database, special characters are replaced with a �.

Example:

The database contains the following text : test 'éï' test
(verified with phpadmnin it exists)

if ( $stmt = $mysqli->prepare ( "SELECT act_omschr FROM jag_activiteiten" ) )
{   
    $stmt->execute();
    $stmt->store_result();  
    $stmt->bind_result ( $DBomschrijving );
    $stmt->fetch();

    if ( $stmt->num_rows )
    {
        echo "$DBomschrijving";
    }

    $stmt->close();
}

As a result i get the following: test ���� test.

Any way to fix this?

Edits :

Changed my database tables to utf8_unicode_ci but it did not fix the issue. But combined with DDA's awnser below it did the trick.

$mysqli = new mysqli ( $loginURL, $dbusername, $dbpassword, $database );    
$mysqli->set_charset ( "utf8" );)

Solution

  • for the same problem with PDO i use
    $bdd=new PDO("mysql:host=$hostname_conn;dbname=$database_conn","$username_conn","$password_conn"); $bdd->EXEC('SET CHARACTER SET utf8');

    ... but your database has to be in utf-8