How to use MySQLi driver in Codeigniter

Hi, I am new to this and I would like to ask someone here who is an expert in the codeigniter framework and PHP.

How can I use mysqli drivers in my own php request? For instance.

My code is:

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 reporting that "mysqli_query expects at least 2 parameters." Is there a way in codeigniter to pass a link to the first mysqli_query () parameter, as described in the php.net documentation http://php.net/manual/en/mysqli.query.php

+4
source share
4 answers

go to:

yourproject/application/config/database.php

change:

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

with:

$db['default']['dbdriver'] = 'mysqli';
+13
source

DB_driver.php codeigniter. :

system/database/DB_driver.php

:

var $dbdriver = 'mysql'; 

to:

var $dbdriver = 'mysqli'; 

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'; 
+2

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

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

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

Scroll to the line

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

Change it as below

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

All Articles