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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25093825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:39:30  来源:igfitidea点击:

How to use MySQLi Driver in Codeigniter

phpcodeignitermysqli

提问by blessedthang

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

嗨,我对此还不太熟悉,我想问这里的一些 codeigniter 框架和 PHP 专家。

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

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

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

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

回答by codematrix

go to:

去:

yourproject/application/config/database.php

change:

改变:

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

with:

和:

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

回答by Cosmo Arun

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

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

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

回答by Mostafa Talebi

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

您应该更改DB_driver.phpcodeigniter 系统目录中文件的内容。去:

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

这是提取的 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'; 

回答by Samuel Anand

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

打开文件夹' /application/config/'下的文件' database.php'

Move to the line

移动到行

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

Modify it as below

修改如下

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