0

我正在从我的数据库中执行选择查询,如下所示:

SELECT DISTINCT r.room_type_id,  rt.room_type_name FROM ROOM as r JOIN ROOM_TYPE as rt 
    ON r.room_type_id = rt.room_type_id Where r.homestead_id = 1
    AND (SELECT Count(*) FROM TENANT as t WHERE r.room_id = t.room_id) < room_capacity 
    AND (room_gender = 'male' OR room_gender='both')

它返回:

+--------------+----------------+
| room_type_id | room_type_name |
+--------------+----------------+
|            1 | Two-bedroom    |
+--------------+----------------+

这正是我想要的。但是当我通过我的 php 运行它时:

function getAvailableRoomTypes($homesteadID,$gender){
    global $con;
    $roomtypes = array();
    //this gets only available rooms
    $sql="SELECT DISTINCT r.room_type_id,  rt.room_type_name FROM ROOM as r JOIN ROOM_TYPE as rt 
    ON r.room_type_id = rt.room_type_id Where r.homestead_id = $homesteadID
    AND (SELECT Count(*) FROM TENANT as t WHERE r.room_id = t.room_id) < room_capacity 
    AND (room_gender = '$gender' OR room_gender='both')";
    $result  = mysqli_query($con, $sql);
    while($row = mysqli_fetch_array($result)){
        $roomtypes += $row;
    }
    var_dump($roomtypes);
    return $roomtypes;
}

我为我的 var_dump 得到这个:

array(4) { [0]=> string(1) "1" ["room_type_id"]=> string(1) "1" [1]=> string(11) "Two-bedroom" ["room_type_name"]=> string(11) "Two-bedroom" }

我期待只收到这个:

array(4) {["room_type_id"]=> string(1) "1" ["room_type_name"]=> string(11) "Two-bedroom" }

那额外的信息是什么?我怎样才能摆脱它,所以我只能得到纯粹的信息?

之后我会将数组转换为 JSON 对象,并且我希望它只有必要的部分。

4

1 回答 1

1

您可以使用mysqli_fetch_assoc而不是mysql_fetch_array.

于 2013-11-15T05:35:22.760 回答