.php文件的字符编码错误 [英] Character encoding error for .php file

查看:118
本文介绍了.php文件的字符编码错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为MarkersController.php创建了一个返回json的路由,但是当我导航到路由时,出现以下错误:

HTML文档的字符编码没有被宣布。如果文档包含US-ASCII范围之外的字符,则该文档将在某些浏览器配置中呈现乱码文本。必须在文档或传输协议中声明页面的字符编码。



我的路线如下所示:

  $ 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');

函数getMarkersJSON(){
//打开到MySQL服务器的连接。
$ connection = mysqli_connect($ server,$ username,$ password);
$ b $ if(!$ connection){
die('Not connected:'。mysqli_error());}
//设置活动的MySQL数据库。
$ db_selected = mysqli_select_db($ database,$ connection);

if(!$ db_selected){
die('can not use db:'。mysqli_error());}

//选择全部标记表中的行。
$ 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)){
//假设lat是测试仪表中的列名称。如果需要请更改它。
$ lat = $ rows ['lat'];
//假设lng是测试仪表中的列名称。如果需要请更改它。
$ lng = $ rows ['lng'];
$ markers = array('lat'=> $ lat,'lng'=> $ lng);
}
echo json_encode($ markers);
}
?>
< / body>
< / html>


解决方案

您是否有Content-Type定义。 HTML?
如果答案是否定的,试着把

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

放入您的head.html


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>

解决方案

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

这篇关于.php文件的字符编码错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆