Extracting CodeIgniter's database access class_PHP tutorial
Well, due to organizational needs, I have recently started to switch to PHP again. The business logic is okay, mainly because the boss requires login verification to be added to the data access layer.
In fact, this requirement is reasonable. Internet services require the upper layer to protect the lower layer, but the lower layer cannot fully trust the upper layer. But the problem arises. There are two options:
1. Write a mysql proxy server to assemble the request from the caller and then return it to the calling side. The main difficulty in doing this is:
a) Assembly and serialization of SQL statements
b) Data set serialization. Although there are many products in this area, it is still too complicated and there is no time to fiddle with it
Give up decisively.
2. Encapsulate a layer of mysql API, and the caller can directly call it locally. In this case, you only need to consider the assembly of SQL statements. There are many choices now,
a) Use a model class similar to Model in django
b) Use Active Record in ci
Although the Model method is better at shielding the data layer, the team members generally believe that this method is too heavy. If it is lighter, they finally chose AR in CodeIgniter.
OK, now, it’s time to test whether the ci module splitting is good or not!
I won’t go into the details of the hard work involved, but let’s just talk about my final implementation: copy system database to a separate directory, x:/php/.
Create a file myconfig.php:
define('BASEPATH', dirname(__FILE__).'/');
define('EXT', '.php');
require_once(BASEPATH . 'database/DB' . EXT);
function &instantiate_class(&$class_object)
{
Return $class_object;
}
function log_message($level = 'error', $message, $php_error = FALSE)
{
echo($message);
}
function MYDB()
{
$params = array(
'dbdriver' => 'mysql',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'dante',
'pconnect' => TRUE,
'db_debug' => FALSE,
'cache_on' => FALSE,
'char_set' => 'utf-8',
'dbcollat' => 'utf8_general_ci',
);
$db = DB($params,TRUE);
Return $db;
}
?>
Create a test file test.php:
require_once('myconfig.php');
$db = MYDB();
$db->select('ID,user_login,user_email');
$query = $db->get('wp_users');
echo "n";
foreach ($query->result() as $row)
{
Print $row->ID . "n";
Print $row->user_login . "n";
Print $row->user_email . "n";
}
?>
The input results are as follows:
Database Driver Class Initialized
1
admin
zny2008@gmail.com
OK~~~ In this way, if we need to perform permission verification before data access, we only need to make a judgment in the MYDB function.
In addition, I have to say that the ci module split is really good. instantiate_class comes from its systemcodeigniterCommon.php. I rewrote log_message because each caller wants to write logs in a different way. (For example, I printed it directly on the screen this time...), I happened to be looking at the design pattern recently, and this method is also in line with the template method pattern.
Author "wolf's personal space"

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

AI Hentai Generator
Generate AI Hentai for free.

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



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an
