php 常用的数据库查询方式:
//根据where 条件查询,使用select()方法
访问:http://localhost/thinkphp2/index.php/Machine/search_ip/?ip=192.168.32.101
返回:
array (size=2)
0 =>
array (size=4)
'sn' => string '63' (length=2)
'ip' => string '192.168.32.101' (length=14)
'info' => string 'dr-mysql' (length=8)
'env' => string 'env-backup' (length=10)
1 =>
array (size=4)
'sn' => string '221' (length=3)
'ip' => string '192.168.32.101' (length=14)
'info' => string 'testscan' (length=8)
'env' => string 'aa' (length=2)
控制层程序:
class machineAction extends Action {
public function search_ip(){
$user=M('machine');
//返回数组
//$arr=$user->select();
$where['ip'] = $_GET['ip'];
$Machine = M('Machine_info');
$arr = $Machine->where($where)->select();
dump($arr);
//$this->assign('list',$arr);
$this->show();
}
}
//根据where 条件查询,使用find()方法,只返回一条:
访问:
http://localhost/thinkphp2/index.php/Machine/search_ip/?ip=192.168.32.101
只返回一条:
array (size=4)
'sn' => string '63' (length=2)
'ip' => string '192.168.32.101' (length=14)
'info' => string 'dr-mysql' (length=8)
'env' => string 'env-backup' (length=10)
控制层程序:
<?php
// 本类由系统自动生成,仅供测试用途
class machineAction extends Action {
public function search_ip(){
$user=M('machine');
//返回数组
//$arr=$user->select();
$where['ip'] = $_GET['ip'];
$Machine = M('Machine_info');
$arr = $Machine->where($where)->find();
dump($arr);
//$this->assign('list',$arr);
$this->show();
}
}
//组查询,然后按ip排序
http://localhost/thinkphp2/index.php/Machine/search_env/env/env-zjtest3
控制层程序:
public function search_env(){
$Machine = M('Machine_info');
//返回数组
//$arr=$user->select();
$where['env'] = $_GET['env'];
$arr = $Machine->where($where)->order('ip')->select();
dump($arr);
//$this->assign('list',$arr);
$this->show();
}
//范围查询
http://localhost/thinkphp2/index.php/Machine/search_range?startip=10&stopip=20
控制层程序:
public function search_range(){
$m = M('Machine_info');
//返回数组
//$arr=$user->select();
$startIp = $_GET['startip'];
$stopIp = $_GET['stopip'];
$arr = $m->query("select * from machine_info where substring_index(ip,'.',-1)>".$startIp." and substring_index(ip,'.',-1)<".$stopIp);
dump($arr);
//$this->assign('list',$arr);
$this->show();
}
}