예배 규칙서 찾다
Array Array Helper Benchmarking Benchmarking Class Caching Caching Driver Calendaring Calendaring Class CAPTCHA CAPTCHA Helper Config Config Class Cookie Cookie Helper Database Connecting to your Database Custom Function Calls Database Caching Class Database Configuration Database Forge Class Database Metadata Database Quick Start: Example Code Database Reference Database Utility Class DB Driver Reference Generating Query Results Queries Query Builder Class Query Helper Methods Transactions Date Date Helper Directory Directory Helper Download Download Helper Email Email Class Email Helper Encrypt Encrypt Class Encryption Encryption Library File File Helper File Uploading File Uploading Class Form Form Helper Form Validation Form Validation FTP FTP Class Functions compatibility_functions common_functions HTML HTML Helper HTML Table HTML Table Class Image Manipulation Image Manipulation Class Inflector Inflector Helper Input Input Class Javascript Javascript Class Language Language Class Language Helper Loader Loader Class Migrations Migrations Class Number Number Helper Output Output Class Pagination Pagination Class Path Path Helper Security Security Class Security Helper Session Session Library Shopping Cart Shopping Cart Class Smiley Smiley Helper String String Helper Template Parser Template Parser Class Text Text Helper Trackback Trackback Class Typography Typography Class Typography Helper Unit Testing Unit Testing Class URI URL User Agent XML XML-RPC and XML-RPC Server Zip Encoding Zip Encoding Class XML-RPC and XML-RPC Server Classes XML Helper User Agent Class URL Helper URI Class
문자

下面的页面包含示例代码,显示如何使用数据库类。要获得完整的细节,请阅读描述每个功能的各个页面。

初始化数据库类

下面的代码根据配置设置:

$this->load->database();

一旦加载,类就可以使用了,如下所述。

注意:如果您的所有页面都需要数据库访问,您可以自动连接。见连接详细信息。

具有多个结果的标准查询(对象版本)

$query = $this->db->query('SELECT name, title, email FROM my_table');foreach ($query->result() as $row){ echo $row->title; echo $row->name; echo $row->email;}echo 'Total Results: ' . $query->num_rows();

上面的result()函数返回一个对象数组。例如:$ row-> title

具有多个结果的标准查询(数组版本)

$query = $this->db->query('SELECT name, title, email FROM my_table');foreach ($query->result_array() as $row){ echo $row['title']; echo $row['name']; echo $row['email'];}

上面的result_array()函数返回一个标准数组索引数组。例如:$ row'title'

具有单一结果的标准查询

$query = $this->db->query('SELECT name FROM my_table LIMIT 1');$row = $query->row();echo $row->name;

上面的row()函数返回一个对象。例如:$ row-> name

具有单一结果的标准查询(阵列版本)

$query = $this->db->query('SELECT name FROM my_table LIMIT 1');$row = $query->row_array();echo $row['name'];

上面的row_array()函数返回一个数组。例如:$ row'name'

标准插入

$sql = "INSERT INTO mytable (title, name) VALUES (".$this->db->escape($title).", ".$this->db->escape($name).")"; $this->db->query($sql);echo $this->db->affected_rows();

查询生成器查询

查询构建器模式为您提供了一种检索数据的简化方法:

$query = $this->db->get('table_name');foreach ($query->result() as $row){ echo $row->title;}

上面的get()函数从提供的表中检索所有结果。查询生成器类包含完整的函数处理数据。

查询生成器插入

$data = array( 'title' => $title, 'name' => $name, 'date' => $date); $this->db->insert('mytable', $data); // Produces: INSERT INTO mytable (title, name, date) VALUES ('{$title}', '{$name}', '{$date}')
이전 기사: 다음 기사: