***CI查询辅助函数:insert_id()、affected_rows()

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: 查询辅助函数 $this->db->insert_id() 这个ID号是执行数据插入时的ID。 $this->db->affected_rows() Displays the number of affected rows, when doing "write\" type queries (insert, update, etc.). 当执行写入操作(insert,update等)的查询后,显示被影响的行数。

查询辅助函数

$this->db->insert_id()


这个ID号是执行数据插入时的ID。

$this->db->affected_rows()

Displays the number of affected rows, when doing "write\" type queries (insert, update, etc.).


当执行写入操作(insert,update等)的查询后,显示被影响的行数。

Note: In MySQL "DELETE FROM TABLE" returns 0 affected rows. The database class has a small hack that allows it to return the correct number of affected rows. By default this hack is enabled but it can be turned off in the database driver file.


注意:在 MySQL 中“DELETE FROM TABLE”的被影响行数将会返回 0。database 类有一个小 hack 允许返回正确的被影响的行数。默认情况下这个 hack 功能是打开的但可以在数据库驱动文件中关闭它。

$this->db->count_all();

Permits you to determine the number of rows in a particular table. Submit the table name in the first parameter. Example:


计算出指定表的总行数并返回。在第一个参数中写入被提交的表名。例如: echo $this->db->count_all('my_table');

// Produces an integer, like 25

$this->db->platform()

Outputs the database platform you are running (MySQL, MS SQL, Postgres, etc...):

输出系统使用的数据库平台(MySQL, MS SQL, Postgres……)

echo $this->db->platform();

$this->db->version()

Outputs the database version you are running:


输出系统正在运行的数据库版本号 echo $this->db->version();

$this->db->last_query();

Returns the last query that was run (the query string, not the result). Example:

返回最后运行的查询(是查询语句,不是查询结果)

$str = $this->db->last_query();

// Produces: SELECT * FROM sometable....

The following two functions help simplify the process of writing database INSERTs and UPDATEs.


下面的两个函数简化了写入数据库的insert和update操作。

$this->db->insert_string();

This function simplifies the process of writing database inserts. It returns a correctly formatted SQL insert string. Example:


这个函数简化了写入数据库的insert函数。它返回一个标准的SQL insert字符串。例如: $data = array('name' => $name, 'email' => $email, 'url' => $url);

$str = $this->db->insert_string('table_name', $data);

The first parameter is the table name, the second is an associative array with the data to be inserted. The above example produces:


第一个参数是表名,第二个是被插入数据的联合数组,上面的例子生成的效果为: INSERT INTO table_name (name, email, url) VALUES ('Rick', 'rick@example.com', 'example.com')

Note: Values are automatically escaped, producing safer queries.


注解:被插入的数据会被自动转换和过滤,生成安全的查询语句。

$this->db->update_string();

This function simplifies the process of writing database updates. It returns a correctly formatted SQL update string. Example:


这个函数简化了写入数据库的update操作。它返回一条格式正确的SQL update字符串。例如: $data = array('name' => $name, 'email' => $email, 'url' => $url);

$where = "author_id = 1 AND status = 'active'";

$str = $this->db->update_string('table_name', $data, $where);

The first parameter is the table name, the second is an associative array with the data to be updated, and the third parameter is the "where" clause. The above example produces:


第一个参数是表名,第二个是被更新数据的关联数组,第三个参数是“where”子句。上面的例子生成的效果为: UPDATE table_name SET name = 'Rick', email = 'rick@example.com', url = 'example.com' WHERE author_id = 1 AND status = 'active'

Note: Values are automatically escaped, producing safer queries.


注解:被插入的数据会被自动转换和过滤,生成安全的查询语句。

如何联系我:【万里虎】www.bravetiger.cn 【QQ】3396726884 (咨询问题100元起,帮助解决问题500元起) 【博客】http://www.cnblogs.com/kenshinobiy/
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
1月前
|
关系型数据库 索引
select for update 含义
select for update 含义
|
9月前
|
SQL 数据库
INSERT DESC UPDATE SELECT
INSERT DESC UPDATE SELECT
68 0
|
6月前
|
数据库 OceanBase
使用 `INSERT INTO table_name SELECT * FROM table_name` 这种方式
使用 `INSERT INTO table_name SELECT * FROM table_name` 这种方式
45 1
|
11月前
insert和insertselective的区别
insert和insertselective的区别
138 0
|
SQL 关系型数据库 MySQL
Select for update使用详解
前言 近期开发与钱相关的项目,在高并发场景下对数据的准确行有很高的要求,用到了for update,故总结一波以便日后留恋。 for update的使用场景 如果遇到存在高并发并且对于数据的准确性很有要求的场景,是需要了解和使用for update的。 比如涉及到金钱、库存等。一般这些操作都是很长一串并且是开启事务的。如果库存刚开始读的时候是1,而立马另一个进程进行了update将库存更新为0了,而事务还没有结束,会将错的数据一直执行下去,就会有问题。所以需要for upate 进行数据加锁防止高并发时候数据出错。
2121 0
|
关系型数据库 PostgreSQL
PostgreSQL merge insert(upsert/insert into on conflict) 如何区分数据是INSERT还是UPDATE
标签 PostgreSQL , merge insert , upsert , insert into on conflict , 区分 insert update , xmin , xmax 背景 使用insert into on conflict update语法,可以支持UPSERT的功能,但是到底这条SQL是插入的还是更新的呢?如何判断 通过xmax字段的值是否不为0,可以判断,如果是UPDATE,XMAX里面会填充更新事务号。
2049 0
|
关系型数据库 MySQL 索引
浅谈create table as 和 insert into select 复制表遇到的问题
之前做一次表压缩测试,在准备原表时需要数据量比较大的表,通过insert into select 的方式将几个表的数据复制到一个表,产生的一些问题~
3130 0