实例:
首先我有一张users表和posts表如下:
CREATE TABLE `users` 如果想要了解更多,你可以去后盾人学习,应该会有不错的收获
一:find函数是所有模型都具备的一个获取数据的多功能函数,使用语法为:find($type, $params)。$type可以是'all','first','count','list','neighbors'或者'threaded'的其中之一。默认情况下会使用'first'进行查找,即会查出表的第一条符合的数据;本人觉得find("all",条件)或者findByUsername("username名字")/findById(id号)这几种用起来比较顺手,实用点。
实例:
首先我有一张users表和posts表如下:
CREATE TABLE `users` (
`id` int(10) NOT NULL auto_increment,
`username` varchar(50) collate utf8_unicode_ci NOT NULL,
`password` varchar(50) collate utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=6
--
-- Dumping data for table `users`
--
INSERT INTO `users` (`id`, `username`, `password`) VALUES
(1, 'admin', '4a7d1ed414474e4033ac29ccb8653d9b'),
(2, 'lht', '4a7d1ed414474e4033ac29ccb8653d9b'),
(3, 'test', 'ca5e6aa29fa7ef71e2655b7f797b98e5')
CREATE TABLE `posts` (
`id` int(11) NOT NULL auto_increment,
`user_id` int(11) NOT NULL,
`title` varchar(255) collate utf8_unicode_ci NOT NULL,
`body` text collate utf8_unicode_ci,
`created` datetime default NULL,
`modified` datetime default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=7
--
-- Dumping data for table `posts`
--
INSERT INTO `posts` (`id`, `user_id`, `title`, `body`, `created`, `modified`) VALUES
(2, 1, 'admin''s message', 'Hi,I''m admin.', '2011-06-21 02:06:40', '2011-06-21 02:06:40'),
(3, 3, 'test', 'Just for a test.', '2011-06-21 02:07:44', '2011-06-21 02:07:44'),
(4, 3, 'user1''s title', 'Just for test.', '2011-06-21 02:13:32', '2011-06-21 02:13:32'),
(5, 1, 'user3''title', 'test', '2011-06-21 02:18:39', '2011-06-21 02:18:39'),
(6, 1, '测试', '测试', '2011-06-22 08:43:06', '2011-06-22 08:43:06')
然后我在users控制器的方法test中执行如下语句:
$result = $this->User->find("all",array('id','username','password'))
$this->set("result",$result)
再在test.ctp视图中打印print_r($result ),将可以看到查询数据表的信息如下:
Array
(
[0] =>Array
(
[User] =>Array
(
[id] =>1
[username] =>admin
[password] =>4a7d1ed414474e4033ac29ccb8653d9b
)
)
[1] =>Array
(
[User] =>Array
(
[id] =>2
[username] =>lht
[password] =>4a7d1ed414474e4033ac29ccb8653d9b
)
)
[2] =>Array
(
[User] =>Array
(
[id] =>3
[username] =>test
[password] =>ca5e6aa29fa7ef71e2655b7f797b98e5
)
)
)
上面所述是查询数据库表所有数据的方法,假如我要查询字段username是admin的,那么我可以用findByUsername(字段名)来查询,如下:
$result = $this->User->findByUsername("admin")然后再打印时将只有username是admin的数据显示,效果如下:
Array
(
[User] =>Array
(
[id] =>1
[username] =>admin
[password] =>4a7d1ed414474e4033ac29ccb8653d9b
)
)
查询admin上面那条语句也可以这样写:$result = $this->User->find('all',array('conditions'=>array('username'=>'admin'))),这样也能达到一样的效果,不过我觉得还是findbyusrname()好用点;
假如我们想查询的结果是字段username不是admin的,那要怎么写呢?很简单,需要用到“<>”这个符号,记得该符号前面要留空格,语句如下:
$result = $this->User->find("all",array('conditions'=>array('username <>'=>'admin')))
此时打印时admin的那一条数据将不显示,其余都显示:
Array
(
[0] =>Array
(
[User] =>Array
(
[id] =>2
[username] =>lht
[password] =>4a7d1ed414474e4033ac29ccb8653d9b
)
)
[1] =>Array
(
[User] =>Array
(
[id] =>3
[username] =>test
[password] =>ca5e6aa29fa7ef71e2655b7f797b98e5
)
)
)
二:query()函数:
query()函数被设计用来执行自定义的SQL语句,语法query("A"),A即正常使用的sql语句。在query()执行的返回数据中,使用数据表名作为key而非模型(model)名。
实例:
1:向users表中插入一条数据:
$pass = md5(0000)
$this->User->query("insert into `mycake`.`users` (`id`,`username`,`password`) values(null,'adam_li','$pass')")
2:将字段username为admm_li的语句密码更改为0000:
$this->User->query("update `mycake`.`users` set `password`='0000' where `users`.`username`='adam_li'")
3:删除字段username为admm_li的语句:
$this->User->query("delete from `mycake`.`users` where `users`.`username`='adam_li'")
4:用联合查询查出users表与posts表id相同,结果要users表中的所有内容,posts表的body字段:
在test方法中执行:
$this->set("select",$this->User->query("select `mycake`.`users`.*,`mycake`.`posts`.`body` from `users` left join `posts` on `users`.`id`=`posts`.`id`"))
在test.ctp视图中打印$select:<pre><?php print_r($select)?></pre>,将显示如下:
Array
(
[0] =>Array
(
[users] =>Array
(
[id] =>1
[username] =>admin
[password] =>4a7d1ed414474e4033ac29ccb8653d9b
)
[posts] =>Array
(
[body] =>
)
)
[1] =>Array
(
[users] =>Array
(
[id] =>2
[username] =>lht
[password] =>4a7d1ed414474e4033ac29ccb8653d9b
)
[posts] =>Array
(
[body] =>Hi,I'm admin.
)
)
[2] =>Array
(
[users] =>Array
(
[id] =>3
[username] =>test
[password] =>ca5e6aa29fa7ef71e2655b7f797b98e5
)
[posts] =>Array
(
[body] =>Just for a test.
)
)
)
注意users表的键值显示为users不是模型User。
保证文件的编码,浏览器解析编码。数据库数据输出编码一致就可以了。先把mysql的数据库编码设为utf8。
另存为保存文件编码为utf8
使用header("content-type:text/htmlcharset=utf-8")或者<meta http-equiv="content-type" content="text/htmlcharset=utf-8" />告诉浏览器解析文件的编码
数据输出前 mysql_query("set names utf8")将数据库输出到网页中的编码设为utf8.
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)