dongwen2162 2016-08-25 13:50
浏览 72


I have already database connected in config folder database.php file. Is there any variable that can simply refers to that MySQLi connection, so that i can use the connection and do manual insertion like-

mysqli_query($alreadyconn, "insert into table ...");

insert() in

I know how to insert in codeigniter but i need the above specific case. and here i can not reconnect that database and use the variable.

Any Help Much Appreciated !

  • 写回答

4条回答 默认 最新

  • dongying7667 2016-08-25 16:36

    Ok. Seems you know about database.php. Will get know some more

    $db['default'] = array(
        'dsn'   => '',
        'hostname' => 'localhost', # host
        'username' => '', # Username
        'password' => '', # password
        'database' => '', # Databse name
        'dbdriver' => 'mysqli', # this can be MySQL or MySQLi

    Note: Recommend to use MySQLi as db driver.

    How to connect database??

    There are Three ways to archive this

    1. Gobally - (path - application/config/autoload.php)

      $autoload['libraries'] = array('database');
    2. Within the class only.

      class ClassName extends CI_Model 
          public function  __construct()

      Can use in all the methods added within this class

    3. Withing Methods only. (Mostly on controllers only)

      class ClassName extends CI_Controller {
          public function  __construct()
          public function index()

      Can use inside the class only

    What is Query Builder (3.0+) or Active Records(3.0-) ??

    This pattern allows information to be retrieved, inserted, and updated in your database with minimal scripting. In some cases only one or two lines of code are necessary to perform a database action. CodeIgniter does not require that each database table be its own class file. It instead provides a more simplified interface.

    How to query with the database & (Query Builder or Active Records) ??

    First Load DB in autoload.php mention above(personally recommended).
    Where ever you are in model or controller just use $this->db to get open connection with your DB.
    With that you can add Query Builder methods like mention in the codeigniter docs. if you are like to use traditional SQL method, you feel free to use query() (Which is my favorite).


    $this->db->query("SELECT * FROM user WHERE name = 'Sparatn' AND ... OR... ");

    But this not enough to produce meaningful data. So you have to follow these steps as well

    $query = $this->db->query("SELECT * FROM user WHERE name = 'Sparatn' "); # 1
    $result  = $query->result_array(); # 2
    return $result; #3

    In #1 assign the database row data to an variable.
    In #2 Convert the #1 to objective array(result_array objective array represent by this).
    In #3 return the result to controller.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?