-1

For the code below how do I output the MySQL query with UTF-8 support ?

<?php
$servername = "###";
$username = "###";
$password = "###";
$dbname = "###";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$sql = "SELECT Column1, Column2, Column3 FROM table ORDER BY Column1 DESC";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    echo '<table class="">';
    echo "<tr><th>Column1</th><th>Column2</th></tr>";
    while($row = mysqli_fetch_assoc($result)) {
        echo "<tr><td>";
        echo '<a href="' . $row['Column1'] . '" target="_blank">' . $row['Column3'] . '</a>';
        echo "</td><td>";   
        echo $row['Column2'];
        echo "</td>";
    }
} else {
    echo "Error.";
}
    echo "</table>";

mysqli_close($conn);
?>

What are the options for a clean output of UTF-8 characters? I tried several solutions found here and on other forums but I gave up. The database Collation is utf8_unicode_ci

Tony
  • 51
  • 8
  • Please check [UTF-8 all the way through](http://stackoverflow.com/questions/279170/utf-8-all-the-way-through). Search in page for `mysqli`. – Álvaro González Apr 29 '15 at 14:08

1 Answers1

1

After $conn = mysqli_connect(..) add this Code

mysqli_query($conn, "SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'"); 
David Demetradze
  • 1,361
  • 2
  • 12
  • 18