CodeIgniter 使用数据库
像任何其他框架一样,我们需要经常与数据库交互,而 CodeIgniter 使我们可以轻松完成这项工作。它提供了丰富的功能来与数据库进行交互。
在本节中,我们将了解 CRUD(创建、读取、更新、删除)函数如何与 CodeIgniter 配合使用。我们将使用 stud 表来选择、更新、删除和插入 stud 表中的数据。
| 表名:stud | |
| roll_no | int(11) |
| name | varchar(30) |
连接到数据库
我们可以通过以下两种方式连接到数据库:
- 自动连接-可以使用文件 application/config/autoload.php 完成自动连接。自动连接将为每个页面加载数据库。我们只需要添加如下所示的数据库库-
$autoload['libraries'] = array(‘database’);
- 手动连接-如果您只想为某些页面连接数据库,那么我们可以手动连接。我们可以通过在任何类中添加以下行来手动连接到数据库。
$this->load->database();
在这里,我们没有传递任何参数,因为一切都在数据库配置文件 application/config/database.php 中设置
插入记录
要在数据库中插入一条记录,使用 insert() 函数,如下表所示:
|
语法 |
insert([$table = ''[, $set = NULL[, $escape = NULL]]]) |
|
Parameters |
|
|
Returns |
成功为真,失败为假 |
|
Return Type |
布尔 |
以下示例显示如何在 stud 表中插入记录。 $data 是一个数组,我们在其中设置了数据并将这些数据插入到表 stud 中,我们只需要将这个数组传递给第 2 nd 中的插入函数sup> 参数。
$data = array(
'roll_no' => ‘1’,
'name' => ‘Virat’
);
$this->db->insert("stud", $data);
更新记录
为了更新数据库中的记录, update() 函数与 set() 和 where() 函数一起使用,作为如下表所示。 set() 函数将设置要更新的数据。
|
语法 |
set($key[, $value = ''[, $escape = NULL]]) |
|
Parameters |
|
|
Returns |
CI_DB_query_builder 实例(方法链) |
|
Return Type |
CI_DB_query_builder |
where() 函数将决定更新哪条记录。
|
语法 |
where($key[, $value = NULL[, $escape = NULL]]) |
|
Parameters |
|
|
Returns |
DB_query_builder 实例 |
|
Return Type |
对象 |
最后, update() 函数将更新数据库中的数据。
|
语法 |
update([$table = ''[, $set = NULL[, $where = NULL[, $limit = NULL ]]]]) |
|
Parameters |
|
|
Returns |
成功为真,失败为假 |
|
Return Type |
布尔 |
$data = array(
'roll_no' => ‘1’,
'name' => ‘Virat’
);
$this->db->set($data);
$this->db->where("roll_no", ‘1’);
$this->db->update("stud", $data);
删除记录
要删除数据库中的记录,使用 delete() 函数,如下表所示:
|
语法 |
delete([$table = ''[, $where = ''[, $limit = NULL[, $reset_data = TRUE]]]]]) |
|
Parameters |
|
|
Returns |
CI_DB_query_builder 实例(方法链)或失败时 FALSE |
|
Return Type |
mixed |
使用以下代码删除 stud表中的一条记录。第一个参数表示要删除记录的表名,第二个参数决定删除哪条记录。
$this->db->delete("stud", "roll_no = 1");
选择记录
要选择数据库中的记录,使用 get函数,如下表所示:
|
语法 |
get([$table = ''[, $limit = NULL[, $offset = NULL]]]) |
|
Parameters |
|
|
Returns |
CI_DB_result 实例(方法链) |
|
Return Type |
CI_DB_result |
使用以下代码从数据库中获取所有记录。第一条语句从"stud"表中获取所有记录并返回对象,该对象将存储在 $query 对象中。第二条语句使用 $query 对象调用 result() 函数以获取所有记录为数组。
$query = $this->db->get("stud");
$data['records'] = $query->result();
关闭连接
可以通过执行以下代码手动关闭数据库连接:
$this->db->close();
示例
创建一个名为 Stud_controller.php 的控制器类并将其保存在 application/controller/Stud_controller.php
这是一个完整的示例,其中执行了所有上述操作。在执行以下示例之前,请按照本章开头的说明创建数据库和表,并在存储在 application/config/database.php的数据库配置文件中进行必要的更改。
<?php
class Stud_controller extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->helper('url');
$this->load->database();
}
public function index() {
$query = $this->db->get("stud");
$data['records'] = $query->result();
$this->load->helper('url');
$this->load->view('Stud_view',$data);
}
public function add_student_view() {
$this->load->helper('form');
$this->load->view('Stud_add');
}
public function add_student() {
$this->load->model('Stud_Model');
$data = array(
'roll_no' => $this->input->post('roll_no'),
'name' => $this->input->post('name')
);
$this->Stud_Model->insert($data);
$query = $this->db->get("stud");
$data['records'] = $query->result();
$this->load->view('Stud_view',$data);
}
public function update_student_view() {
$this->load->helper('form');
$roll_no = $this->uri->segment('3');
$query = $this->db->get_where("stud",array("roll_no"=>$roll_no));
$data['records'] = $query->result();
$data['old_roll_no'] = $roll_no;
$this->load->view('Stud_edit',$data);
}
public function update_student(){
$this->load->model('Stud_Model');
$data = array(
'roll_no' => $this->input->post('roll_no'),
'name' => $this->input->post('name')
);
$old_roll_no = $this->input->post('old_roll_no');
$this->Stud_Model->update($data,$old_roll_no);
$query = $this->db->get("stud");
$data['records'] = $query->result();
$this->load->view('Stud_view',$data);
}
public function delete_student() {
$this->load->model('Stud_Model');
$roll_no = $this->uri->segment('3');
$this->Stud_Model->delete($roll_no);
$query = $this->db->get("stud");
$data['records'] = $query->result();
$this->load->view('Stud_view',$data);
}
}
?>
创建一个名为 Stud_Model.php 的模型类并将其保存在 application/models/Stud_Model.php
<?php
class Stud_Model extends CI_Model {
function __construct() {
parent::__construct();
}
public function insert($data) {
if ($this->db->insert("stud", $data)) {
return true;
}
}
public function delete($roll_no) {
if ($this->db->delete("stud", "roll_no = ".$roll_no)) {
return true;
}
}
public function update($data,$old_roll_no) {
$this->db->set($data);
$this->db->where("roll_no", $old_roll_no);
$this->db->update("stud", $data);
}
}
?>
创建一个名为 Stud_add.php 的视图文件并将其保存在 application/views/Stud_add.php
<!DOCTYPE html>
<html lang = "en">
<head>
<meta charset = "utf-8">
<title>Students Example</title>
</head>
<body>
<?php
echo form_open('Stud_controller/add_student');
echo form_label('Roll No.');
echo form_input(array('id'=>'roll_no','name'=>'roll_no'));
echo "<br/>";
echo form_label('Name');
echo form_input(array('id'=>'name','name'=>'name'));
echo "<br/>";
echo form_submit(array('id'=>'submit','value'=>'Add'));
echo form_close();
?>
</body>
</html>
创建一个名为 Stud_edit.php 的视图文件并将其保存在 application/views/Stud_edit.php
<!DOCTYPE html>
<html lang = "en">
<head>
<meta charset = "utf-8">
<title>Students Example</title>
</head>
<body>
<form method = "" action = "">
<?php
echo form_open('Stud_controller/update_student');
echo form_hidden('old_roll_no',$old_roll_no);
echo form_label('Roll No.');
echo form_input(array('id'⇒'roll_no',
'name'⇒'roll_no','value'⇒$records[0]→roll_no));
echo "
";
echo form_label('Name');
echo form_input(array('id'⇒'name','name'⇒'name',
'value'⇒$records[0]→name));
echo "
";
echo form_submit(array('id'⇒'sub mit','value'⇒'Edit'));
echo form_close();
?>
</form>
</body>
</html>
创建一个名为 Stud_view.php 的视图文件并将其保存在 application/views/Stud_view.php
<!DOCTYPE html>
<html lang = "en">
<head>
<meta charset = "utf-8">
<title>Students Example</title>
</head>
<body>
<a href = "<?php echo base_url(); ?>
index.php/stud/add_view">Add</a>
<table border = "1">
<?php
$i = 1;
echo "<tr>";
echo "<td>Sr#</td>";
echo "<td>Roll No.</td>";
echo "<td>Name</td>";
echo "<td>Edit</td>";
echo "<td>Delete</td>";
echo "<tr>";
foreach($records as $r) {
echo "<tr>";
echo "<td>".$i++."</td>";
echo "<td>".$r->roll_no."</td>";
echo "<td>".$r->name."</td>";
echo "<td><a href = '".base_url()."index.php/stud/edit/"
.$r->roll_no."'>Edit</a></td>";
echo "<td><a href = '".base_url()."index.php/stud/delete/"
.$r->roll_no."'>Delete</a></td>";
echo "<tr>";
}
?>
</table>
</body>
</html>
在 application/config/routes.php 的路由文件中进行以下更改,并在文件末尾添加以下行。
$route['stud'] = "Stud_controller"; $route['stud/add'] = 'Stud_controller/add_student'; $route['stud/add_view'] = 'Stud_controller/add_student_view'; $route['stud/edit/(\d+)'] = 'Stud_controller/update_student_view/$1'; $route['stud/delete/(\d+)'] = 'Stud_controller/delete_student/$1';
现在,让我们通过在浏览器中访问以下 URL 来执行此示例。将 yoursite.com 替换为您的网址。
http://yoursite.com/index.php/stud
下一章:CodeIgniter 库
CodeIgniter 框架的基本部分是它的库。它提供了丰富的库集,间接提高了应用程序的开发速度。系统库位于 system/libraries。我们需要做的就是加载我们想要使用的库。可以如下所示加载库 ...
AI 中文社