mysqli fetch_object在结果集数组的第一个元素中返回非对象

时间:2014-02-06 09:41:24

标签: php mysql arrays mysqli

我有一个特殊的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
        )

2 个答案:

答案 0 :(得分:1)

试试这个......

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

     print_r($projAreas);

答案 1 :(得分:0)

试试这个

$projAreas= array();

而不是

$projAreas[]= array();

你已声明数组$projAreas并按[]初始化它,它取第一个索引0