Zend framework中的增删改查常用方法

fetchAll($where,$order,$count,$offset)->toArray();
		
		echo "
";
		print_r($res);
		echo "
"; //显示计算机系的前三个学生 //显示前三个学生的信息,按照年龄排序 $where="sdept='计算机'"; $order='sage'; $count=3; $offset=0; //创建表模型 echo "

计算机

"; //$studentModel=new Student(); $res=$studentModel->fetchAll($where,$order,$count,$offset)->toArray(); echo "
";
		print_r($res);
		echo "
"; //考虑sql注入问题 //创建适配器 $db=$studentModel->getAdapter(); $where=$db->quoteInto("sdept=?",'计算机'); //注入考虑,一步一步注入 $db=$studentModel->getAdapter(); $where=$db->quoteInto("sdept=?",'计算机').$db->quoteInto(" AND ssex=?",F); //取出所有学生的名字和性别 $db=$studentModel->getAdapter(); //$sql=$db->quoteInto("select sname,ssex from student"); //不能这样用 //$sql=$db->quoteInto("select sname,ssex from student where ssex=:sex and sdept=:dept",array('sex'=>'F','dept'=>'计算机')); $res=$db->query("select sname,ssex from student where ssex=:sex and sdept=:dept",array('sex'=>'F','dept'=>'计算机'))->fetchAll(); //$res=$db->query($sql)->fetchAll();//使用适配器完成查询数据,不用toArray() echo "

~~~~~·~~~~~~~~

"; echo "
";
		print_r($res);
		echo "
"; //增加一条数据 $course=new Course(); $data=array( 'cid'=>'56', 'cname'=>'java编程', 'ccredit'=>87 ); $course->insert($data); //修改数据 $set=array( 'ccredit'=>99 ); $where="cid='55'"; $course->update($set,$where); //删除一条数据 $course->delete($where); } public function testAction(){ $studentModel=new Student(); $db=$studentModel->getAdapter(); //根据主键取出shuju //$stu=$studentModel->find("20041010")->toArray(); $stu=$studentModel->find(array("20041010","20041011"))->toArray(); echo "
";
		print_r($stu);
		echo "
"; //取出一条记录,这样比较快捷 $res=$studentModel->fetchRow("saddress='天津'","ssex")->toArray(); echo "

只查询一条记录

"; echo "
";
		print_r($res);
		echo "
"; //取出不重复的数据 //什么时候使用表模型=增加,删除,查询全部字段,什么时候使用适配器=查询部分字段,多表查询,防止注入漏洞 $res=$db->query("select distinct sdept from student")->fetchAll(); echo "

只查询!!!!!

"; echo "
";
		print_r($res);
		echo "
"; //查询广州和香港的学生 $res=$db->query("select * from student where saddress in('广州','香港')")->fetchAll(); echo "

只查询广州和香港的学生

"; echo "
";
		print_r($res);
		echo "
"; //查询计算机和会计的学生 $res=$db->query("select sdept,avg(sage) from student group by sdept")->fetchAll(); echo "

只查询广州和香港的学生

"; echo "
";
		print_r($res);
		echo "
"; $this->render('show'); } }

初始化数据库适配器

db);
		 $db->query('SET  NAMES  UTF8');
		 Zend_Db_Table::setDefaultAdapter($db);
	}
}
?>
对应数据库中的course表,其中主键为cid
对应数据库中student表,其中主键为sid
数据库配置:
[production]
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
appnamespace = "Application"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.params.displayExceptions = 0

[mysql]
db.adapter=PDO_MYSQL
db.params.host=localhost
db.params.username=root
db.params.password=root
db.params.dbname=stusys
student表的创建
create table student(
sid char(8) primary key,
sname varchar(64) not null default '',
ssex char(2) not null defalut '女',
sdept varchar(32) not null default '',
sage tinyint unsigned default 0,
saddress varchar(64) not null default '');
course表的创建
create table course(
cid char(2) primary key,
cname varchar(64) not null default '',
ccredit tinyint unsigned not null default 0);


你可能感兴趣的:(Zend,Framework)