MySQLi:fetch_object

时间:2011-03-10 23:37:28

标签: php multidimensional-array mysqli

我的类下面的方法生成一个多维数组,但我只需要一维数组,

# return the current row of a result set as an object
    public function fetch_object($query) 
    {
        $result = $this->connection->query($query);
        if($result)
        {
            $function_result = array();
            $i = 0;
            while($row = $result->fetch_object())
            {
                # you should store each row in an array and then return the array
                $function_result[$i] = $row;
                $i++;
            }
            return $function_result;
        }
        else
        {
            return $this->get_error();
        }
    }
例如,

,它创建,

Array
(
    [0] => stdClass Object
        (
            [cnt_id] => 1
            [cnt_email1] => xx@yahoo.co.uk
            [cnt_email2] => 
            [cnt_fullname] => Lau x
            [cnt_firstname] => x
            [cnt_lastname] => Lau
            [cnt_created] => 2011-02-04 00:00:00
            [cnt_updated] => 2011-02-04 13:53:49
        )

)

但我实际上只需要这个,

Array
(
    [cnt_id] => 1
    [cnt_email1] => xx@yahoo.co.uk
    [cnt_email2] => 
    [cnt_fullname] => Lau x
    [cnt_firstname] => x
    [cnt_lastname] => Lau
    [cnt_created] => 2011-02-04 00:00:00
    [cnt_updated] => 2011-02-04 13:53:49
)

如何修复代码以仅生成一维数组?

感谢。

1 个答案:

答案 0 :(得分:0)

抱歉,我的错误,我应该这样写,

# return the current row of a result set as an object
    public function fetch_object($query) 
    {
        $result = parent::query($query);
        if($result)
        {
            return $result->fetch_object();
        }
        else
        {
            # call the get_error function
            return self::get_error();
            # or:
            # return $this->get_error();
        }

    }