I have set up a table in my database to load records from a json file that include special characters. I am using the following PHP code to upload the records.
<!DOCTYPE html>
<html>
<head>
<title>Insert Records</title>
</head>
<body>
<h1>Insert OER Records from JSON File</h1>
<h2>Outputs:</h2>
<?php
ini_set('memory_limit','2000M');
$url = 'path/filename.json';
$contents = file_get_contents($url);
$contents = utf8_encode($contents);
$results = json_decode($contents, true);
include 'connection.php';
// Counter for number of records (x)
$x = 0;
foreach($results as $key => $value) {
$x = $x + 1;
foreach($value as $k => $v) {
$type = $value['type'];
$title = $value['title'];
$title = str_replace("'", "\'", $title);
$title = str_replace("(", "\(", $title);
$title = str_replace(")", "\)", $title);
$author = $value['author'];
$author = str_replace("'", "\'", $author);
$author = str_replace("(", "\(", $author);
$author = str_replace(")", "\)", $author);
$link = $value['link'];
$source = $value['source'];
$source = str_replace("'", "\'", $source);
$source = str_replace("(", "\(", $source);
$source = str_replace(")", "\)", $source);
$description = str_replace("'", "\'", $description);
$description = str_replace("(", "\(", $description);
$description = str_replace(")", "\)", $description);
$description = $value['description'];
$base_url = $value['base_url'];
$isbn_number = $value['isbn_number'];
$e_isbn_number = $value['e_isbn_number'];
$publication_date = $value['publication_date'];
$license = $value['license'];
$subject = $value['subject'];
$image_url = $value['image_url'];
$review = $value['review'];
$language = $value['language'];
$loc_collection = $value['loc_collection'];
$license_url = $value['image_url'];
}
$sql = "INSERT INTO oer_search (type, title, author, link, source, description, base_url, isbn_number, e_isbn_number, publication_date, license, subject, image_url, review, language, license_url) VALUES ('$type', '$title', '$author', '$link', '$source', '$description', '$base_url', '$isbn_number', '$e_isbn_number', '$publication_date', '$license', '$subject', '$image_url', '$review', '$language', '$license_url')";
if (mysqli_query($conn, $sql)) {
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
}
echo "This many records were entered into the database: " . $x;
mysqli_close($conn);
?>
<br/><br/><br/><br/><br/>
</body>
</html>
I have my database set to utf8_general_ci. However, the records with special characters are not displaying correctly. I am getting results like this MuhÌ£ammad Kurd Ê»AliÌ
. Is there something I am missing?