How to use MySQLi Driver in Codeigniter

假如想象 提交于 2020-01-10 02:50:11

问题


Hi I'm kinda new to this and I would like to ask someone here that are experts in codeigniter framework and PHP.

How can I use mysqli drivers in php native query? For example.

My code:

Model

class Home_model extends CI_Model{

public function getusers(){
$q = "SELECT * FROM `users`";
return $r = mysqli_query($q);
}

}

Controller:

class Home extends CI_Controller{
public function iterateuser(){
while($row = mysqli_fetch_object($this->Home_model->getusers())){
echo $row->username;
}

}

My code above is an error, report saying "mysqli_query expects at least 2 parameters". Is there a way in codeigniter to pass the link on the mysqli_query() first parameter as described on the php.net documentation http://php.net/manual/en/mysqli.query.php


回答1:


go to:

yourproject/application/config/database.php

change:

$db['default']['dbdriver'] = 'mysql';   

with:

$db['default']['dbdriver'] = 'mysqli';



回答2:


Once you have specified 'mysqli', you can use the following function to get mysqli. For example, mysqli_real_escape_str, etc.:

function get_mysqli() { 
    $db = (array)get_instance()->db;
    return mysqli_connect('localhost', $db['username'], $db['password'], $db['database']);
}



回答3:


You should change the content of the file DB_driver.php in you codeigniter system directory. Go to:

system/database/DB_driver.php

And change the following line:

var $dbdriver = 'mysql'; 

to :

var $dbdriver = 'mysqli'; 

here is a extraction of the DB_driver.php

 * MySQLi Database Adapter Class - MySQLi only works with PHP 5
 *
 * Note: _DB is an extender class that the app controller
 * creates dynamically based on whether the active record
 * class is being used or not.
 *
 * @package        CodeIgniter
 * @subpackage    Drivers
 * @category    Database
 * @author        ExpressionEngine Dev Team
 * @link        http://ellislab.com/codeigniter/user-guide/database/
 */
class CI_DB_mysqli_driver extends CI_DB {

    var $dbdriver = 'mysqli'; 



回答4:


Open the file 'database.php' under the folder '/application/config/'

Move to the line

$db['default'] = array('dbdriver' => '')

Modify it as below

$db['default'] = array('dbdriver' => 'mysqli')


来源:https://stackoverflow.com/questions/25093825/how-to-use-mysqli-driver-in-codeigniter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!