Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php CodeIgniter活动记录返回Mysql数据类型,而不仅仅是字符串_Php_Mysql_Codeigniter_Types_Sqldatatypes - Fatal编程技术网

Php CodeIgniter活动记录返回Mysql数据类型,而不仅仅是字符串

Php CodeIgniter活动记录返回Mysql数据类型,而不仅仅是字符串,php,mysql,codeigniter,types,sqldatatypes,Php,Mysql,Codeigniter,Types,Sqldatatypes,我使用的是codeIgniter版本2.0.3。我想知道是否有一种简单的方法可以返回具有正确数据类型的数据。例如,对于模式: +-------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+--------------+------+-----+---------+----------

我使用的是codeIgniter版本2.0.3。我想知道是否有一种简单的方法可以返回具有正确数据类型的数据。例如,对于模式:

+-------+--------------+------+-----+---------+----------------+
| Field | Type         | Null | Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
| id    | int(11)      | NO   | PRI | NULL    | auto_increment |
| term  | varchar(255) | NO   |     | NULL    |                |
| level | int(11)      | NO   |     | NULL    |                |
+-------+--------------+------+-----+---------+----------------+
活动记录
结果\u数组
返回:

array(4) {
  [0]=>
  array(3) {
    ["id"]=>
    string(1) "1"
    ["term"]=>
    string(11) "Mesoamerica"
    ["level"]=>
    string(1) "1"
  }
  [1]=>
  array(3) {
    ["id"]=>
    string(1) "2"
    ["term"]=>
    string(6) "Mexico"
    ["level"]=>
    string(1) "1"
  }
  [2]=>
  array(3) {
    ["id"]=>
    string(1) "3"
    ["term"]=>
    string(10) "indigenous"
    ["level"]=>
    string(1) "2"
  }
  [3]=>
  array(3) {
    ["id"]=>
    string(1) "4"
    ["term"]=>
    string(5) "ruins"
    ["level"]=>
    string(1) "2"
  }
}

这样做的目的是让类型在结果集上通过<代码>ID应该是
整数
级别
应该是
整数
。我通常只是键入我需要使用的数据。我在文档中没有看到任何将此应用于codeIgniter的方法,但我希望有一种简单的方法可以做到这一点。我已经在PDO中看到了这样做的方法,但是codeIgniter是否为我提供了一些方便?

我制作了一个助手:

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * cast_fields
 *
 * Casts string values provided from database into
 * the appropriate datatype of possible.
 */
if ( ! function_exists('cast_fields'))
{
    function cast_fields($fields, $results)
    {
        for($i=0; $i<count($fields); $i++) {
            $name = $fields[ $i ]->name;
            $type = $fields[ $i ]->type;
            $int_types = array(
                'int',
                'tinyint',
                'smallint',
                'mediumint',
                'bigint'
            );
            if(in_array($type, $int_types)) {
                for($j=0; $j<count($results); $j++) {
                    $casted = intval( $results[ $j ]->{ $name } );
                    $results[ $j ]->{ $name } = $casted;
                }
            }
        }
        return $results;
    }

}

/* End of file field_data_helper.php */
/* Location: ./application/helpers/field_data_helper.php */
// Get rows from database.
$query = $this->db->get_where( $this->table, $criteria, $limit, $offset );
$results = $query->result();

// Cast data to the correct datatypes.
$fields = $this->db->field_data( $this->table );
$this->load->helper('field_data_helper');
$results = cast_fields($fields, $results);

我采取了这种不幸的方法:我只是更改了
/system/database/DB_Result.php
文件

方法
自定义结果\u对象
中的
行85
如下所示:

$object->$key = $value; 
将其更改为:

if(is_numeric($value)) $object->$key = (int)$value;
else $object->$key = $value;
然后将此方法添加到任意位置:

/**
* Scans a rowset to cast valid ints as integers instead of strings
*/
private function recast($row)
{
    foreach($row as $key => $value)
    {
        if(is_object($row)) {
            if(is_numeric($value)) $row->{$key} = (int)$value;
        } else {
            if(is_numeric($value)) $row[$key] = (int)$value;
        }

    }

    return $row;
} 
然后在
result\u object()中找到

$this->result_object[] = $row;
$this->result_array[] = $row;
在这条线上,加上

$row = $this->recast($row);
$row = $this->recast($row);
$n = $this->recast($n);
$n = $this->recast($n);
$n = $this->recast($n);
然后在
result\u array()中找到

$this->result_object[] = $row;
$this->result_array[] = $row;
在这条线上,加上

$row = $this->recast($row);
$row = $this->recast($row);
$n = $this->recast($n);
$n = $this->recast($n);
$n = $this->recast($n);
然后在
自定义行\u对象()
中,找到

$this->current_row = $n;
$this->current_row = $n;
$this->current_row = $n;
在这条线上,加上

$row = $this->recast($row);
$row = $this->recast($row);
$n = $this->recast($n);
$n = $this->recast($n);
$n = $this->recast($n);
然后在
行\u object()
中,找到

$this->current_row = $n;
$this->current_row = $n;
$this->current_row = $n;
在这条线上,加上

$row = $this->recast($row);
$row = $this->recast($row);
$n = $this->recast($n);
$n = $this->recast($n);
$n = $this->recast($n);
然后在
行数组()中找到

$this->current_row = $n;
$this->current_row = $n;
$this->current_row = $n;
在这条线上,加上

$row = $this->recast($row);
$row = $this->recast($row);
$n = $this->recast($n);
$n = $this->recast($n);
$n = $this->recast($n);
当然,重铸函数可以根据您的解析需要进行修改和更新,现在它只是在需要的地方强制转换整数,而不需要使用正则表达式。这种方法使日期时间戳保持不变,但任何被认为是数值的东西都会被解析。 还可以进行一些额外的浮动检查


我只需要对API函数执行此操作,因为json_encode将我所有的整数强制转换为硬字符串,
id:“1”
而不是int
id:1
,并且需要一个能够反映整个站点的解决方案。

如果使用
result()
而不是
result_array()
然后返回一个对象。@xbonez我刚试过,认为我可能遗漏了什么,它仍然返回所有内容,如stringsread此处:@YanBerk您的链接似乎已失效,这应该是新的url这是一个很好的解决方案,直到您使用连接:(