CI 获取数据库数据类型总是为STRING 的解决方案
来源:互联网 发布:algorithm基础算法 编辑:程序博客网 时间:2024/06/06 09:11
This isn’t something I have time to do today, but for anyone else who comes across this problem, you can probably enable json_encodeto output numbers from active records with the following steps:
1) Modify the mysql/mysql_resultphp file to include the$field->numeric metadata attribute.
2) Add an extra step to active record queries that uses CodeIgniter’s $this->db->field_data() totest for any fields with numeric==1
3) If numeric==1, cast that field’s value to a number.
function field_data()
{
$retval = array();
while ( $field = mysql_fetch_field($this->result_id))
{
$F = new stdClass();
$F->name = $field->name;
$F->type = $field->type;
$F->default = $field->def;
$F->max_length = $field->max_length;
$F->primary_key = $field->primary_key;
$F->numeric = $field->numeric; // <!-- ADD THIS LINE
$retval[] = $F;
}
return $retval;
}
Heavy reliance on JSON in my scenario might make the extra effortworthwhile, but most people might never run into this.
2、It’s a blunt wayto solve the problem, but “good enough” for me. The functioncomments are based on PHP’s settype() documentationat
<?php
function cast_fieldtypes($record, $fieldTypes) {
foreach($record as $fieldName => $value) {
if(isset($fieldTypes[$fieldName])) {
$type = $fieldTypes[$fieldName];
$value = $record[$fieldName];
switch ( $type) {
case 'boolean':
case 'bool':
$value = (bool) $value;
break;
case 'integer':
case 'int':
$value = (int) $value;
break;
case 'float':
$value = (float) $value;
break;
case 'string':
$value = (string) $value;
break;
default:
break;
}
$record[$fieldName] = $value;
}
}
return $record;
}
?>
Here’s an exampleof how to use it:
// Declare the types for some of the fields (not necessarily all of them)
$fieldTypes = array(
'weight'=>'int',
'revision'=>'int',
'is_visible'=>'bool',
);
$record = $this->db->get_where('content', array( 'id'=>1));
$casted_record = cast_fieldtypes($record, $fieldTypes);
echo json_encode($casted_record);
- CI 获取数据库数据类型总是为STRING 的解决方案
- android中webview接收favicon总是获取为null解决方案
- android中webview接收favicon总是获取为null解决方案
- 在实体类中将数据库中数据类型为CLOB的数据转化成String类型
- CI数据库的相关操作&获取客户端IP
- CI的数据库操作
- 实体类数据类型 String类数据 插入或查询oracle 数据库中字段类型为CLOB的字段
- CI框架获取数据库错误码
- Js获取fileupload的绝对路径时总是的到C:\fakepath\+文件名称的 解决方案
- weblogic获取路径总是为NULL
- ci 对数据库的操作
- String转换为其他数据类型
- 【CI学习笔记】CI显示数据库数据的实例
- 得到站点的绝对地址--weblogic获取路径总是为NULL
- 数据库的表总是死锁
- Delphi中string数据类型的特殊用法(获取string字符串的单个字符)
- ibatis的数据库值为空的错误(number数据类型)
- String,无所不在的数据类型
- CPC、CPM、CPA、CPS
- NSUserDefaults not get saved sometime
- 生产者消费者模型
- MYSQL to SQLite 脚本
- 不建议使用rowid作为sqlite主键
- CI 获取数据库数据类型总是为STRING 的解决方案
- CCLabelTTF with fixed width and dynamic height solution
- Erlang性能的八个误区(Efficiency Guide)
- MYSQL DB SYN
- NAT穿透
- Socket服务器整体架构概述
- uva10422(Knights in FEN)-bfs+哈希(隐式图搜索)
- MYSQL LIMIT NOT WORK WHEN ESTIMATE NUMBER OF ROWS
- CCSPRITE:Load image from users documents folder