【问题标题】:mysqli fetch_object returning non-object in the first element of the result set arraymysqli fetch_object 在结果集数组的第一个元素中返回非对象
【发布时间】:2014-03-03 04:03:20
【问题描述】:

我有一个特定的 mysqli 查询,它在结果集数组的第 0 个位置返回一个空元素。当我尝试循环显示结果集以显示到屏幕时,这个空的非对象元素似乎会导致问题

$projAreas[] = array();
$projectID = $_GET['projectID'];

$sql = "SELECT *
       FROM  `areas` ,  `project_area_junc` 
       WHERE  `areas`.`areaID` =  `project_area_junc`.`areaID` 
        AND  `project_area_junc`.`projectID` = $projectID";

$results = $conn->query($sql);
while($row = $results->fetch_object()) {
    $projAreas[] = $row;
    }

结果在位置 0 处给出了不需要的数组元素) 即当我 print_r($projAreas) 我明白了:

Array
(
    [0] => Array
        (
        )

    [1] => stdClass Object
        (
            [areaID] => 56
            [propertyID] => 14
            [areaName] => Living Room
            [areaInfo] =>  lots of windows - colonial style
            [proj_area_juncID] => 10
            [projectID] => 4
        )

    [2] => stdClass Object
        (
            [areaID] => 57
            [propertyID] => 14
            [areaName] => Kitchen
            [areaInfo] =>   
            [proj_area_juncID] => 11
            [projectID] => 4
        )

【问题讨论】:

    标签: php mysql arrays mysqli


    【解决方案1】:

    试试这个....

     $projAreas = array();
        while($row = $results->fetch_object()) {
            array_push($projAreas ,$row);
            }
    
         print_r($projAreas);
    

    【讨论】:

      【解决方案2】:

      试试这个

      $projAreas= array();

      而不是

      $projAreas[]= array();

      您已经声明了数组$projAreas,并通过[] 对其进行了初始化,它采用第一个索引0

      【讨论】:

      • 谢谢,这行得通! ,正如您发现的那样,问题是因为我包含了方括号 - 我应该看到,d'oh!
      猜你喜欢
      • 2018-10-03
      • 2014-07-10
      • 1970-01-01
      • 2013-06-21
      • 2014-08-11
      • 2018-03-14
      • 2014-04-10
      • 1970-01-01
      相关资源
      最近更新 更多