如何在codeigniter中使用已连接数据库的数据库连接?

我已经在配置文件夹 database.php </ code>文件中连接了数据库。
有没有 变量可以简单地引用那个 MySQLi </ code>连接,这样我就可以使用连接并进行手动插入,如 - </ p>

  mysqli_query($ alreadyconn,“ 插入表...“); 
</ code> </ pre>

insert()</ code> </ p>

我知道如何在codeigniter中插入,但我需要上述特定情况 。 在这里,我无法重新连接该数据库并使用该变量。</ p>

任何帮助都得到了很多赞赏!</ p>
</ div>

展开原文

原文

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 Codeigniter.com

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个回答

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()
        {
            parent::__construct();
            $this->load->database();
        } 
    }
    

    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()
        {
            parent::__construct();
        }
    
        public function index()
        {
            $this->load->database();
        }
    

    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).

Example

$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.

To Insert Data In Database you must do not need MySQL function in code igniter . You should have to use

     $this->db->insert() 

function to store data in database . For More Help kindly visit https://www.formget.com/insert-data-into-database-using-codeigniter/ Thank You

duanqian6982
duanqian6982 但不是视野或控制者
大约 4 年之前 回复
douling8087
douling8087 是的,你可以使用mysqli功能
大约 4 年之前 回复
ds78662302
ds78662302 谢谢你的回答。 我知道我可以使用$ this-> db-> insert()插入数据库。 但我想插入使用MySQL功能。 我不能直接在Code Igniter中使用MySQL功能吗?
大约 4 年之前 回复

Use $this->db to refer to your db connection

$query = $this->db->query("
        YOUR QUERY HERE
");

None really answered to the question. The question is clear: how to recycle the codeigniter database connection to be used with a native mysqli_query function without using mysqli_connect again. The upvoted answers above aren't helpful by any means to the issuer's question.

Use $this->db->conn_id as database connection reference in your $alreadyconn variable.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问