如何在Codeigniter中使用MySQLi驱动程序

时间:2014-08-02 10:02:26

标签: php codeigniter mysqli

嗨,我对此有点新意,我想问一下这里有代码签名框架和PHP的专家。

如何在php原生查询中使用mysqli驱动程序?例如。

我的代码:

模型

class Home_model extends CI_Model{

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

}

控制器:

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

}

我上面的代码是一个错误,报告说" mysqli_query期望至少有2个参数"。在codeigniter中是否有一种方法可以传递mysqli_query()第一个参数上的链接,如php.net文档中所述http://php.net/manual/en/mysqli.query.php

4 个答案:

答案 0 :(得分:16)

转到:

yourproject/application/config/database.php

改变:

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

使用:

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

答案 1 :(得分:3)

指定'mysqli'后, 你可以使用以下函数来获取mysqli。例如,mysqli_real_escape_str等:

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

答案 2 :(得分:2)

您应该更改codeigniter系统目录中文件DB_driver.php的内容。转到:

system/database/DB_driver.php

并更改以下行:

var $dbdriver = 'mysql'; 

到:

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

答案 3 :(得分:0)

在“ / application / config / ”文件夹下打开“ database.php ”文件

移至

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

将其修改如下

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