在codeinighter框架中,怎么直接断开客户端的连接?
假设有个用户发文章的场景。
一个用户表,一个类别表,一个文章表,文章表有个user_id关联用户表,一个category_id关联类别表。用mysql的数据库,没有做外键约束。
当用户发布一篇文章的时候,我需要去更新类别表的post_count字段,表示这个类别下有多少篇文章之类的。因为考虑到数据完整性要求不那么高,所以我这里没放在一个事务中去更新两张表的数据,而是分别去更新每张表的数据。
我希望能够在插入文章表之后就返回给客户端,创建成功的信息,断开与客户端的连接,然后再完成后续更新类别表的post_count字段。
lz之前是写python的,tornado框架就有个概念叫is_finish,标明当前与客户端的连接是否已经断开了,不知道codeinighter里面有没类似的概念呢?或者说怎么样主动断开与客户端的连接?
PS:lz也不知道这种不用事务的做法是不是不太合适,没真实在产品环境上跑过.....
回复内容:
假设有个用户发文章的场景。
一个用户表,一个类别表,一个文章表,文章表有个user_id关联用户表,一个category_id关联类别表。用mysql的数据库,没有做外键约束。
当用户发布一篇文章的时候,我需要去更新类别表的post_count字段,表示这个类别下有多少篇文章之类的。因为考虑到数据完整性要求不那么高,所以我这里没放在一个事务中去更新两张表的数据,而是分别去更新每张表的数据。
我希望能够在插入文章表之后就返回给客户端,创建成功的信息,断开与客户端的连接,然后再完成后续更新类别表的post_count字段。
lz之前是写python的,tornado框架就有个概念叫is_finish,标明当前与客户端的连接是否已经断开了,不知道codeinighter里面有没类似的概念呢?或者说怎么样主动断开与客户端的连接?
PS:lz也不知道这种不用事务的做法是不是不太合适,没真实在产品环境上跑过.....
http是无状态协议,PHP是URL驱动的脚本语言,没有守护进程,不支持异步。
用户访问一个PHP页面,一般情况下,Apache会单开一个进程来处理这个页面,处理完毕进程退出,nginx会发送一个请求给一个PHP守护进程,同样的,会新开一个进程或线程,处理完毕立刻退出。
如果想异步,可以用redis等存储队列、另开守护进程处理队列。
根据题主的情况,建议一次完成。
你说的应该是指返回数据后,php再继续在后台处理剩余数据,你这个需求在php中应该实现不了,php这门语言在http通信结束后,就会回收当前通信过程中使用到的所有局部变量,所以你这个需求很有可能实现不了。
其实你的这个需求是属于异步处理,这是node.js的强项。
水平不足,没看明白断开客户端连接的意思。
不过可以给你推荐两个ci的查询辅助函数,如下:
$this->db->insert_id()
执行数据插入时的ID,也就是返回插入成功后的ID$this->db->affected_rows()
当执行写入操作(insert,update等)的查询后,显示被影响的行数。
相关文档的链接在此:http://codeigniter.org.cn/user_guide/database/helpers.html
利用这两个函数应该足够确认插入数据成功了,希望能帮到你 : )

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











MySQL and phpMyAdmin can be effectively managed through the following steps: 1. Create and delete database: Just click in phpMyAdmin to complete. 2. Manage tables: You can create tables, modify structures, and add indexes. 3. Data operation: Supports inserting, updating, deleting data and executing SQL queries. 4. Import and export data: Supports SQL, CSV, XML and other formats. 5. Optimization and monitoring: Use the OPTIMIZETABLE command to optimize tables and use query analyzers and monitoring tools to solve performance problems.

AI can help optimize the use of Composer. Specific methods include: 1. Dependency management optimization: AI analyzes dependencies, recommends the best version combination, and reduces conflicts. 2. Automated code generation: AI generates composer.json files that conform to best practices. 3. Improve code quality: AI detects potential problems, provides optimization suggestions, and improves code quality. These methods are implemented through machine learning and natural language processing technologies to help developers improve efficiency and code quality.

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.

In MySQL, add fields using ALTERTABLEtable_nameADDCOLUMNnew_columnVARCHAR(255)AFTERexisting_column, delete fields using ALTERTABLEtable_nameDROPCOLUMNcolumn_to_drop. When adding fields, you need to specify a location to optimize query performance and data structure; before deleting fields, you need to confirm that the operation is irreversible; modifying table structure using online DDL, backup data, test environment, and low-load time periods is performance optimization and best practice.

To safely and thoroughly uninstall MySQL and clean all residual files, follow the following steps: 1. Stop MySQL service; 2. Uninstall MySQL packages; 3. Clean configuration files and data directories; 4. Verify that the uninstallation is thorough.

Efficient methods for batch inserting data in MySQL include: 1. Using INSERTINTO...VALUES syntax, 2. Using LOADDATAINFILE command, 3. Using transaction processing, 4. Adjust batch size, 5. Disable indexing, 6. Using INSERTIGNORE or INSERT...ONDUPLICATEKEYUPDATE, these methods can significantly improve database operation efficiency.

MySQL functions can be used for data processing and calculation. 1. Basic usage includes string processing, date calculation and mathematical operations. 2. Advanced usage involves combining multiple functions to implement complex operations. 3. Performance optimization requires avoiding the use of functions in the WHERE clause and using GROUPBY and temporary tables.

Composer is a dependency management tool for PHP, and manages project dependencies through composer.json file. 1) parse composer.json to obtain dependency information; 2) parse dependencies to form a dependency tree; 3) download and install dependencies from Packagist to the vendor directory; 4) generate composer.lock file to lock the dependency version to ensure team consistency and project maintainability.
