简体   繁体   English

$ query-> result_array()擦除list_fields()数据

[英]$query->result_array() wipes away list_fields() data

I'm having an issue where I call the result_array() function on n query object from in codeigniter: 我遇到一个问题,我从codeigniter中的n个查询对象上调用result_array()函数:

   $this->db->select(array('a','b','c'));

   $query = $this->db->get('basic');
   print_r($query->list_fields());
   $test = $query->result_array();
   print_r($query->list_fields());

When I run this code, or: 当我运行此代码时,或:

   $query = $this->db->get('basic');
   print_r($query->list_fields());
   print_r($query->list_fields());

or: 要么:

   $query = $this->db->get('basic');
   $test = $query->result_array();
   print_r($query->list_fields());

The second list_fields() function always returns an array size 0, the first returns the correct list of field names. 第二个list_fields()函数始终返回数组大小0,第一个返回正确的字段名称列表。

In the last example where there is only one list_fields() function, the array is again size zero. 在只有一个list_fields()函数的最后一个示例中,数组的大小再次为零。

Any guidance in this matter will be greatly appreciated. 对此问题的任何指导将不胜感激。 I need the list_fields() function to be accessible after I read the result_array() . 在读取result_array()之后,需要list_fields()函数可访问。

Here is the result of the first block of code: 这是第一段代码的结果:

    Array
    (
        [0] => site_id
        [1] => institution
        [2] => caller
        [3] => call_complete
        [4] => call_details
        [5] => id
        [6] => timestamp
    )
    Array
    (
    )

Thank you, for your help 谢谢您的帮助

That looks to be a bug in the database driver. 看来这是数据库驱动程序中的错误。 For example, in CI_DB_mysqli_result : 例如,在CI_DB_mysqli_result

public function list_fields()
{
    $field_names = array();
    while ($field = $this->result_id->fetch_field())
    {
        $field_names[] = $field->name;
    }

    return $field_names;
}

A subsequent call will return array() because the while loop seeks over all the fields and leaves the pointer at the end of the list. 后续调用将返回array()因为while循环查找所有字段并将指针留在列表的末尾。

However, in the result class, result_id is public, so you can use mysqli_result::field_seek : 但是,在结果类中, result_id是公共的,因此可以使用mysqli_result :: field_seek

$query = $this->db->get('basic');
var_dump($query->list_fields());

// this should be called before any call to list_fields()
$query->result_id->field_seek(0);
var_dump($query->list_fields());

However, this is bad practise, as this only works for mysqli; 但是,这是不好的做法,因为这仅适用于mysqli。 for mysql, you'll need this: 对于mysql,您将需要以下内容:

mysql_field_seek($query->result_id, 0);

and for mssql: 对于mssql:

mssql_field_seek($query->result_id, 0);

The correct way to do this is really to fix it in the database drivers. 正确的方法实际上是在数据库驱动程序中修复它。 See this pull request :-) 看到这个拉请求 :-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM