我的目标是使用 CodeIgniter 数据库类,但在一个普通的 PHP 脚本中。 我不想为此使用 MVC 结构。
我可以使用这个数据库类WITHOUT使用 CodeIgniter MVC 模式吗?
如果是,怎么办?
最佳答案
请按照以下步骤集成 Codeigniter DB Active Record
- 从 https://codeigniter.com/ 下载最新的 codeigniter
复制以下文件
application/config/config.php application/config/database.php system/database/* system/core/Common.php system/core/Exceptions.php system/core/Log.php
目录结构如上
- 在
application/config/database.php
中添加数据库连接细节 创建数据库连接(connector.php)文件
<?php defined('DS') OR define('DS', DIRECTORY_SEPARATOR); defined('EXT') OR define('EXT', '.php'); defined('ENVIRONMENT') OR define('ENVIRONMENT', 'development'); $dir_path = dirname(__FILE__) . DS; defined('BASEPATH') OR define('BASEPATH', $dir_path . 'system' . DS); defined('APPPATH') OR define('APPPATH', $dir_path . 'application' . DS); function getDBConnector(){ include_once(BASEPATH . "core/Common.php"); include_once(BASEPATH . "core/Exceptions.php"); require_once(BASEPATH . 'database/DB' . EXT); $conn = & DB(); return $conn; } $db = getDBConnector(); print_r($db->get('users')->result_array());
- 现在在您的项目中包含 connector.php 并访问 DB 对象:)
关于php - 如何在没有codeIgniter MVC框架的情况下使用数据库codeigniter类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15309976/