关于数据库
时间:2010-03-31 来源:bing_fox
1. 简介
各种数据库操作接口
2.
===最简单的连接
$para2 = array(
'host' => '127.0.0.1',
'username' => 'jack',
'password' => '111111',
'dbname' => 'zftest',
'port' => '3306',
);
$this->db = Zend_Db::factory('PDO_MYSQL',$para2);
===
3. Zend_Db_Table 表的抽象
//设置Zend_Db_Table的默认适配器
Zend_Db_Table::setDefaultAdapter($this->db);
//实现表的抽象,一般在models目录下
Student.php
===
class Student extends Zend_Db_Table {
//重定义表名和主键
protected function _setup()
{
$this->_name = 'student';
$this->_primary = 'id';
parent::_setup();
}
//定义一些方法操作表,或过虑参数
public function add($data){
$this->insert($data);
}
}
===
4. 使用
//将Student类加载进来,之前要在index.php中设置好include的目录models
Zend_Loader::loadClass('Student');
//实例化$db
//设置默认适配器
Student::setDefaultAdapter($this->db);
//实例化表类
$table = new Student();
5. 操作
5.1 insert
5.2 update
===
$set = array('name' => 'aaa',
'password' => md5('111111')
);
$whereAsk = "id = ?";
$whereValue = 1;
$table->updateRow($set, $whereAsk, $whereValue);
public function updateRow($set, $whereAsk, $whereValue){
$db = $this->getAdapter();
$where = $db->quoteInto($whereAsk, $whereValue);
$this->update($set, $where);
}
===
5.3 delete
public function deleteRow($whereAsk, $whereValue){
$db = $this->getAdapter();
$where = $db->quoteInto($whereAsk, $whereValue, "int");
$this->delete($where);
}
5.4 show
===rowset结构
public function showAction()
{
echo "Show";
//where语句, order by name, limit 1, 2
$rowset=$this->table->fetchAll('id <= 17', "name", 2, 1);
foreach ($rowset as $row){
echo "<br>";
echo $row->id;
echo "<br>";
echo $row->name;
echo "<br>";
echo $row->password;
echo "<br>";
echo $row->age;
echo "<br>";
echo $row->email;
}
/* Initialize action controller here */
}
===
===row 结构
$row = $this->table->fetchRow('id = 17', "name");
echo "<br>";
echo $row->id;
echo "<br>";
echo $row->name;
$row->name = "bbb";
$row->save();
===
5.5 另一种修改记录的方法
$row->age = 30;
$row->save();
各种数据库操作接口
2.
===最简单的连接
$para2 = array(
'host' => '127.0.0.1',
'username' => 'jack',
'password' => '111111',
'dbname' => 'zftest',
'port' => '3306',
);
$this->db = Zend_Db::factory('PDO_MYSQL',$para2);
===
3. Zend_Db_Table 表的抽象
//设置Zend_Db_Table的默认适配器
Zend_Db_Table::setDefaultAdapter($this->db);
//实现表的抽象,一般在models目录下
Student.php
===
class Student extends Zend_Db_Table {
//重定义表名和主键
protected function _setup()
{
$this->_name = 'student';
$this->_primary = 'id';
parent::_setup();
}
//定义一些方法操作表,或过虑参数
public function add($data){
$this->insert($data);
}
}
===
4. 使用
//将Student类加载进来,之前要在index.php中设置好include的目录models
Zend_Loader::loadClass('Student');
//实例化$db
//设置默认适配器
Student::setDefaultAdapter($this->db);
//实例化表类
$table = new Student();
5. 操作
5.1 insert
5.2 update
===
$set = array('name' => 'aaa',
'password' => md5('111111')
);
$whereAsk = "id = ?";
$whereValue = 1;
$table->updateRow($set, $whereAsk, $whereValue);
public function updateRow($set, $whereAsk, $whereValue){
$db = $this->getAdapter();
$where = $db->quoteInto($whereAsk, $whereValue);
$this->update($set, $where);
}
===
5.3 delete
public function deleteRow($whereAsk, $whereValue){
$db = $this->getAdapter();
$where = $db->quoteInto($whereAsk, $whereValue, "int");
$this->delete($where);
}
5.4 show
===rowset结构
public function showAction()
{
echo "Show";
//where语句, order by name, limit 1, 2
$rowset=$this->table->fetchAll('id <= 17', "name", 2, 1);
foreach ($rowset as $row){
echo "<br>";
echo $row->id;
echo "<br>";
echo $row->name;
echo "<br>";
echo $row->password;
echo "<br>";
echo $row->age;
echo "<br>";
echo $row->email;
}
/* Initialize action controller here */
}
===
===row 结构
$row = $this->table->fetchRow('id = 17', "name");
echo "<br>";
echo $row->id;
echo "<br>";
echo $row->name;
$row->name = "bbb";
$row->save();
===
5.5 另一种修改记录的方法
$row->age = 30;
$row->save();
相关阅读 更多 +