Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php JSON_ENCODE在AJAX请求中将所有数组值转换为字符串_Php_Mysql_Arrays_Ajax_Json - Fatal编程技术网

Php JSON_ENCODE在AJAX请求中将所有数组值转换为字符串

Php JSON_ENCODE在AJAX请求中将所有数组值转换为字符串,php,mysql,arrays,ajax,json,Php,Mysql,Arrays,Ajax,Json,我执行一个简单的AJAX请求,从mysql数据库中选择一些数据。当我将数组传回Javascript时,它总是将数组中的所有值转换为字符串,而不管其数据类型在数据库中是整数还是布尔值 编辑: 我刚刚发现,MySQLi总是将数据类型转换为字符串,所以我想这就是问题所在。json_编码工作正常 SQL语句 function getAll() { // Get all buildings $db = new db(); $sql = "SE

我执行一个简单的AJAX请求,从mysql数据库中选择一些数据。当我将数组传回Javascript时,它总是将数组中的所有值转换为字符串,而不管其数据类型在数据库中是整数还是布尔值

编辑: 我刚刚发现,MySQLi总是将数据类型转换为字符串,所以我想这就是问题所在。json_编码工作正常

SQL语句

   function getAll()
   {
        // Get all  buildings
        $db = new db();

        $sql = "SELECT * FROM building";
        $result = $db->runQuery($sql);

        $rows = array();
        while($row = $result->fetch_assoc()) {
            $rows[] = $row;
        }     

        return $rows;
   }  
[Object, Object, Object, Object, Obje...]
>0: Object
>1: Object
>2: Object
>3: Object
   ActiveCost: "20"
   BuildEfc: "0"
   BuildSfx: "0"
   BuildingEfc: "0"
   BuildingID: "1"
   Name: "Vogtei I"
   ResidentLvLNeeded: "0"
   ...
PHP控制器文件

function getBuildings(){

  $bu_db = new Building_Model();
  $buildings = $bu_db->getAll();

  echo json_encode($buildings);
}
var data = {
  action: "getBuildings"
};

$.ajax({
  type: "POST",
  dataType: "json",
  url: "controller/Building.php",
  data: data,
  success: function(data) {
    console.log(data);
  },
  error: function(XMLHttpRequest, textStatus, errorThrown) {
     console.log("Error in AJAX Request: " + textStatus + ", " + errorThrown);
  }
});
Javascript文件

function getBuildings(){

  $bu_db = new Building_Model();
  $buildings = $bu_db->getAll();

  echo json_encode($buildings);
}
var data = {
  action: "getBuildings"
};

$.ajax({
  type: "POST",
  dataType: "json",
  url: "controller/Building.php",
  data: data,
  success: function(data) {
    console.log(data);
  },
  error: function(XMLHttpRequest, textStatus, errorThrown) {
     console.log("Error in AJAX Request: " + textStatus + ", " + errorThrown);
  }
});
它不保留数据库中的原始数据类型,而是将所有值转换为字符串:

输出

   function getAll()
   {
        // Get all  buildings
        $db = new db();

        $sql = "SELECT * FROM building";
        $result = $db->runQuery($sql);

        $rows = array();
        while($row = $result->fetch_assoc()) {
            $rows[] = $row;
        }     

        return $rows;
   }  
[Object, Object, Object, Object, Obje...]
>0: Object
>1: Object
>2: Object
>3: Object
   ActiveCost: "20"
   BuildEfc: "0"
   BuildSfx: "0"
   BuildingEfc: "0"
   BuildingID: "1"
   Name: "Vogtei I"
   ResidentLvLNeeded: "0"
   ...
有人知道这个问题吗


提前感谢。

您使用什么PHP版本

尝试:

echo json_编码($buildings,json_NUMERIC_CHECK)


Javascript具有动态数据类型。因此,int字符串等没有特定的数据类型。同一个var对象可以保存所有数据类型。它将根据您对这些变量所做的操作动态标识类型

我使用PHP版本5.5.8。我可以这样做,但有些值实际上是字符串,看起来像整数,我不想将它们转换为整数。没有办法保持原始数据类型吗?(String,Integer,也为NULL)我完全同意这一点,但请看一下php手册页面:在示例部分中,整数值没有加引号。