简体   繁体   English

如何在没有codeIgniter MVC框架的情况下使用数据库codeigniter类?

[英]How to use database codeigniter class without codeIgniter MVC framework?

My goal is to use the CodeIgniter database Class, but inside a plain PHP script. 我的目标是使用CodeIgniter数据库类,但在一个普通的PHP脚本中。 I don't want to use MVC structure for this. 我不想为此使用MVC结构。

Can I use this database class WITHOUT using the CodeIgniter MVC pattern? 我可以在使用CodeIgniter MVC模式的情况下使用此数据库类吗?

If yes, how? 如果有,怎么样?

Please follow below steps to integrate Codeigniter DB Active Record 请按照以下步骤集成Codeigniter DB Active Record

  1. Download latest codeigniter from https://codeigniter.com/ https://codeigniter.com/下载最新的codeigniter
  2. Copy below files 复制下面的文件

     application/config/config.php application/config/database.php system/database/* system/core/Common.php system/core/Exceptions.php system/core/Log.php 

    在此输入图像描述

The directory structure is as above 目录结构如上

  1. Add database connection details in application/config/database.php application/config/database.php添加数据库连接详细信息
  2. Create db connection(connector.php) file 创建数据库连接(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()); 
  3. Now include connector.php in your project and access DB object :) 现在在项目中包含connector.php并访问DB对象:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM