不管是用select 查询数据集,还是用find 查询数据,常配合连贯 *** 作where、field、order等一起使用。
field():用于定义要查询的字段(支持字段排除)
用法:field($field , $except=false)
参数:$field —— 字段名,要查询的字段名;
$except —— 是否排除,默认为false,省略不写;如果为true,表示定义的字段为数据表中排除$field参数定义之外的所有字段
返回值:当前的模型实例
注:不调用field方法,默认返回所有字段,和field( '*' )等效。
需要显式的传入所有的字段,可以使用下面的方法:
$model ->field(true) ->select()
但是建议只获取需要显式的字段名,或者采用字段排除方式来定义,例如:
$model ->field( 'status' , true) ->select() //获取除了status之外的所有字段
$model ->field( 'id','nickname')->select() //获取id和nickname字段
getField():用于查询某个字段的值
用法:getField($field, $sepa=null)
参数:$field —— 要获取的字段字符串(多个用逗号分隔)
$sepa —— 字段数据间隔符号,如果是 NULL返回数组为数组。默认为null
返回值:如果查询结果为空返回null,如果field是一个字段则返回该字段的值,如果field是多个字段,返回数组。数组的索引是第一个字段的值,sepa为null则返回二维数组。
示例:当只有一个字段的时候,默认返回一个值。
$User = M("User")// 实例化User对象
// 获取ID为3的用户的昵称
$nickname = $User->where('id=3')->getField('nickname')
示例:如果需要返回数组:
<?phpclass MysqlManage{
/*创建数据库,并且主键是aid
* table 要查询的表名
*/
function createTable($table){
$sql="CREATE TABLE IF NOT EXISTS `$table` (`aid` INT NOT NULL primary key)ENGINE = InnoDB"
M()->execute($sql)
$this->checkTable($table)
}
/*
* 检测表是否存在,也可以获取表中所有字段的信息
* table 要查询的表名
* return 表里所有字段的信息
*/
function checkTable($table){
$sql="desc `$table`"
$info=M()->execute($sql)
return $info
}
/*
* 检测字段是否存在,也可以获取字段信息(只能是一个字段)
* table 表名
* field 字段名
*/
function checkField($table,$field){
$sql='desc `$table` $field'
$info=M()->execute($sql)
return $info
}
/*
* 添加字段
* table 表名
* info 字段信息数组 array
* return 字段信息 array
*/
function addField($table,$info){
$sql="alter table `$table` add column"
$sql.=$this->filterFieldInfo()
M()->execute($sql)
$this->checkField($table,$info['name'])
}
/*
* 修改字段
* 不能修改字段名称,只能修改
*/
function editField($table,$info){
$sql="alter table `$table` modify "
$sql.=$this->filterFieldInfo($info)
M()->execute($sql)
$this->checkField($table,$info['name'])
}
/*
* 字段信息数组处理,供添加更新字段时候使用
* info[name] 字段名称
* info[type] 字段类型
* info[length] 字段长度
* info[isNull] 是否为空
* info['default'] 字段默认值
* info['comment'] 字段备注
*/
private function filterFieldInfo($info){
if(!is_array($info))
return
$newInfo=array()
$newInfo['name']=$info['name']
$newInfo['type']=$info['type']
switch($info['type']){
case 'varchar':
case 'char':
$newInfo['length']=empty($info['length'])?100:$info['length']
$newInfo['isNull']=$info['isNull']==1?'NULL':'NOT NULL'
$newInfo['default']=empty($info['default'])?'':'DEFAULT '.$info['default']
$newInfo['comment']=empty($info['comment'])?'':'COMMENT '.$info['comment']
break
case 'int':
$newInfo['length']=empty($info['length'])?7:$info['length']
$newInfo['isNull']=$info['isNull']==1?'NULL':'NOT NULL'
$newInfo['default']=empty($info['default'])?'':'DEFAULT '.$info['default']
$newInfo['comment']=empty($info['comment'])?'':'COMMENT '.$info['comment']
break
case 'text':
$newInfo['length']=''
$newInfo['isNull']=$info['isNull']==1?'NULL':'NOT NULL'
$newInfo['default']=''
$newInfo['comment']=empty($info['comment'])?'':'COMMENT '.$info['comment']
break
}
$sql=$newInfo['name']." ".$newInfo['type']
$sql.=(!empty($newInfo['length']))?($newInfo['length']) .' ':' '
$sql.=$newInfo['isNull'].' '
$sql.=$newInfo['default']
$sql.=$newInfo['comment']
return $sql
}
/*
* 删除字段
* 如果返回了字段信息则说明删除失败,返回false,则为删除成功
*/
function dropField($table,$field){
$sql="alter table `$table` drop column $field"
M()->execute($sql)
$this->checkField($table,$filed)
}
/*
* 获取指定表中指定字段的信息(多字段)
*/
function getFieldInfo($table,$field){
$info=array()
if(is_string($field)){
$this->checkField($table,$field)
}else{
foreach($field as $v){
$info[$v]=$this->checkField($table,$v)
}
}
return $info
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)