Extracting database access classes from CodeIgniter _php tutorial

Source: Internet
Author: User
OK, because the organization needs, and recently started to go to PHP, business logic is OK, mainly the boss requires the data access layer to add login authentication.
In fact, this requirement is reasonable, the Internet service requires the upper layer of protection, but the lower layer can not fully believe the upper layer. But here's the problem, like the next two options:
1. Write a MySQL proxy server to assemble the request from the caller and 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, but it is still too complex, and there is no time to toss
Give up decisively.
2. Encapsulate a layer of MySQL API, the caller can be called directly locally. In this case, you only need to consider the consolidation of SQL statements. There's a lot of options right now.
A) using model classes like Django
b) using the active Record in CI
Although the mode of the model, the shielding of the data layer is better, but the team members generally think this way too heavy, a little bit of light, and eventually chose the CodeIgniter in the AR.
OK, so now, Test CI module split the good time to!
Concrete in the middle of all sorts of hard not to say, straight to my final realization put, 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 ~ ~ ~ so we need to do a permission check before the data access, just need to make a judgment in the MyDB function.
In addition, it must be said that the CI module is really good, Instantiate_class is from its system\codeigniter\common.php. Log_message I rewrote it, because for each caller, the way to write log is different. (For example, I printed it directly on the screen this time ....) ), recently just looking at design patterns, this approach is also in line with the template method pattern.

Author "Wolf's Personal Space"

http://www.bkjia.com/PHPjc/478617.html www.bkjia.com true http://www.bkjia.com/PHPjc/478617.html techarticle OK, because the organization needs, and recently started to go to PHP, business logic is OK, mainly the boss requires the data access layer to add login authentication. Actually this kind of request is also reasonable ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.