0

I have a php script that connects to a MySQL database and returns some data in JSON, but the returned data that is in Greek appears as question marks.

I have checked the this question and didn't worked for me or I don't do it correct. What is wrong with it?

<?php
header('content-type: text/html; charset=UTF-8'); 
// Create connection
$con=mysqli_connect("localhost","db_user","db_pass","db_name");

// Check connection
if (mysqli_connect_errno())
{
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

if( function_exists('mysql_set_charset') ){
    mysqli_set_charset('utf8', $con);
}else{
    mysqli_query("SET NAMES 'utf8'", $con);
}

$sql = "SELECT * FROM Locations";


if ($result = mysqli_query($con, $sql))
{
    $resultArray = array();
    $tempArray = array();

    while($row = $result->fetch_object())
    {
        $tempArray = $row;
        array_push($resultArray, $tempArray);
    }

    echo json_encode($resultArray);
}

mysqli_close($con);
?>
Community
  • 1
  • 1
ToroLoco
  • 461
  • 1
  • 5
  • 20

1 Answers1

1

There was a incorrect syntax on the mysqli_set_charset. The correct part of the code is the below:

if( function_exists('mysql_set_charset') ){
    mysqli_set_charset($con, 'utf8');
}else{
    mysqli_query($con, "SET NAMES 'utf8'");
}
ToroLoco
  • 461
  • 1
  • 5
  • 20