Character encoding error for .php file

不打扰是莪最后的温柔 提交于 2019-12-06 03:57:43

问题


Have made a route for MarkersController.php which returns json, but when i navigate to the route I get the following error:

The character encoding of the HTML document was not declared. The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range. The character encoding of the page must be declared in the document or in the transfer protocol.

My route is as follows:

$app->get('/markers/?', function () use ($app) {
    $controller = new UF\MarkersController($app);
    return $controller->getMarkersJSON();
}); 

MarkersController.php

<!DOCTYPE html>
<html lang="en">
    <head>
    {% include 'components/head.html' %}
    </head>
    <body>
<?php
include('DB_INFO.php');

function getMarkersJSON(){     
// Opens a connection to a MySQL server.
$connection = mysqli_connect($server, $username, $password);

if (!$connection) {
    die('Not connected : ' . mysqli_error());}
// Sets the active MySQL database.
$db_selected = mysqli_select_db($database, $connection);

if (!$db_selected) {
    die('Can\'t use db : ' . mysqli_error());}

// Selects all the rows in the markers table.
$query = "SELECT * FROM tester WHERE 1";
$result = mysqli_query($connection, $query);

if (!$result) {
    die('Invalid query: '. mysqli_error());
}
$markers = array();
while ($row = mysqli_fetch_assoc($result)) {
    //Assuming "lat" is column name in tester table. Please change it if required.
    $lat= $rows['lat'];
    //Assuming "lng" is column name in tester table. Please change it if required.
    $lng= $rows['lng'];
    $markers = array('lat' => $lat, 'lng' => $lng);
}
echo json_encode($markers);
}
?>
 </body>
</html>

回答1:


Do you have Content-Type definition into your head.html? If the answer is no, try to put

<meta http-equiv="Content-Type" content="text/html;charset=[your-charset]" />

into your head.html



来源:https://stackoverflow.com/questions/32207755/character-encoding-error-for-php-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!