codeigniter mysql -1_mysql-Codeigniter-多个数据库连接
mysql-Codeigniter-多个数据库连接
我必须从主数据库中检索MySQL数据库信息,然后连接到该数据库,并获取一些记录。
我的意思是持有一个数据库,我想加载另一个数据库。
Codeigniter是否可能? 现在,我在模型中使用以下代码行。
function connectDb($credential)
{
$config['hostname'] = $credential['server'];
$config['username'] = $credential['username'];
$config['password'] = $credential['password'];
$config['database'] = $credential['database'];
$config['dbdriver'] = "mysql";
$config['dbprefix'] = "";
$config['pconnect'] = FALSE;
$config['db_debug'] = TRUE;
$config['cache_on'] = FALSE;
$config['cachedir'] = "";
$config['char_set'] = "utf8";
$config['dbcollat'] = "utf8_general_ci";
$DB2=$this->load->database($config);
$DB2->db->select('first_name,last_name');
$query = $DB2->db->get('person');
print_r($query);
}
它不起作用还有其他办法吗?
6个解决方案
80 votes
您应该在“ application / config / database.php”中提供第二个数据库信息
通常,您将设置$db['default']数据库组,如下所示:
$db['default']['hostname'] = "localhost";
$db['default']['username'] = "root";
$db['default']['password'] = "";
$db['default']['database'] = "database_name";
$db['default']['dbdriver'] = "mysql";
$db['default']['dbprefix'] = "";
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = FALSE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = "";
$db['default']['char_set'] = "utf8";
$db['default']['dbcollat'] = "utf8_general_ci";
$db['default']['swap_pre'] = "";
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
请注意,登录信息和设置在名为$db['default']的数组中提供。
然后,您可以在新数组中添加另一个数据库-我们称其为“ otherdb”。
$db['otherdb']['hostname'] = "localhost";
$db['otherdb']['username'] = "root";
$db['otherdb']['password'] = "";
$db['otherdb']['database'] = "other_database_name";
$db['otherdb']['dbdriver'] = "mysql";
$db['otherdb']['dbprefix'] = "";
$db['otherdb']['pconnect'] = TRUE;
$db['otherdb']['db_debug'] = FALSE;
$db['otherdb']['cache_on'] = FALSE;
$db['otherdb']['cachedir'] = "";
$db['otherdb']['char_set'] = "utf8";
$db['otherdb']['dbcollat'] = "utf8_general_ci";
$db['otherdb']['swap_pre'] = "";
$db['otherdb']['autoinit'] = TRUE;
$db['otherdb']['stricton'] = FALSE;
如何实际使用第二个数据库,您必须将连接发送到可以在模型中使用的另一个变量:
function my_model_method()
{
$otherdb = $this->load->database('otherdb', TRUE); // the TRUE paramater tells CI that you'd like to return the database object.
$query = $otherdb->select('first_name, last_name')->get('person');
var_dump($query);
}
那应该做。可在以下位置找到用于连接多个数据库的文档:[http://codeigniter.com/user_guide/database/connecting.html]
Repox answered 2020-02-04T00:15:38Z
18 votes
最好的方法是使用不同的数据库组。 如果要继续照常使用主数据库($ this-> db),只需关闭辅助数据库的持久连接配置选项即可。 只有master数据库应该与持久连接一起工作:
主数据库
$db['default']['hostname'] = "localhost";
$db['default']['username'] = "root";
$db['default']['password'] = "";
$db['default']['database'] = "database_name";
$db['default']['dbdriver'] = "mysql";
$db['default']['dbprefix'] = "";
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = FALSE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = "";
$db['default']['char_set'] = "utf8";
$db['default']['dbcollat'] = "utf8_general_ci";
$db['default']['swap_pre'] = "";
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;
辅助数据库(注意pconnect设置为false)
$db['otherdb']['hostname'] = "localhost";
$db['otherdb']['username'] = "root";
$db['otherdb']['password'] = "";
$db['otherdb']['database'] = "other_database_name";
$db['otherdb']['dbdriver'] = "mysql";
$db['otherdb']['dbprefix'] = "";
$db['otherdb']['pconnect'] = FALSE;
$db['otherdb']['db_debug'] = FALSE;
$db['otherdb']['cache_on'] = FALSE;
$db['otherdb']['cachedir'] = "";
$db['otherdb']['char_set'] = "utf8";
$db['otherdb']['dbcollat'] = "utf8_general_ci";
$db['otherdb']['swap_pre'] = "";
$db['otherdb']['autoinit'] = TRUE;
$db['otherdb']['stricton'] = FALSE;
然后,您可以照常使用主数据库,而将辅助数据库用作数据库对象:
// use master dataabse
$users = $this->db->get('users');
// connect to secondary database
$otherdb = $this->load->database('otherdb', TRUE);
$stuff = $otherdb->get('struff');
$otherdb->insert_batch('users', $users->result_array());
// keep using master database as usual, for example insert stuff from other database
$this->db->insert_batch('stuff', $stuff->result_array());
Simmoniz answered 2020-02-04T00:16:11Z
14 votes
用这个。
$dsn1 = 'mysql://user:password@localhost/db1';
$this->db1 = $this->load->database($dsn1, true);
$dsn2 = 'mysql://user:password@localhost/db2';
$this->db2= $this->load->database($dsn2, true);
$dsn3 = 'mysql://user:password@localhost/db3';
$this->db3= $this->load->database($dsn3, true);
用法
$this->db1 ->insert('tablename', $insert_array);
$this->db2->insert('tablename', $insert_array);
$this->db3->insert('tablename', $insert_array);
Rayiez answered 2020-02-04T00:16:36Z
11 votes
这对我来说可以...
这是默认数据库:
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'mydatabase',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => TRUE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
在database.php文件的底部添加另一个数据库
$db['second'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'mysecond',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => TRUE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
在autoload.php配置文件中
$autoload['libraries'] = array('database', 'email', 'session');
通过自动加载数据库库,默认数据库可以正常工作 但是第二个数据库通过在模型中使用构造函数来加载和连接 控制器...
class Seconddb_model extends CI_Model {
function __construct(){
parent::__construct();
//load our second db and put in $db2
$this->db2 = $this->load->database('second', TRUE);
}
public function getsecondUsers(){
$query = $this->db2->get('members');
return $query->result();
}
}
?>
Mani answered 2020-02-04T00:17:13Z
6 votes
在查看代码时,我唯一看到的错误是,当您尝试加载第二个数据库时:
$DB2=$this->load->database($config);
当您要检索数据库对象时,必须在第二个参数中传递TRUE。
从Codeigniter用户指南中:
通过将第二个参数设置为TRUE(布尔值),该函数将 返回数据库对象。
因此,您的代码应改为:
$DB2=$this->load->database($config, TRUE);
这将使其工作。
Quetzy Garcia answered 2020-02-04T00:17:55Z
0 votes
If you need to connect to more than one database simultaneously you can do so as follows:
$DB1 = $this->load->database('group_one', TRUE);
$DB2 = $this->load->database('group_two', TRUE);
注意:将单词“ group_one”和“ group_two”更改为您要连接的特定组名(或者您可以如上所示传递连接值)。
通过将第二个参数设置为TRUE(布尔值),该函数将返回数据库对象。
请访问[https://www.codeigniter.com/userguide3/database/connecting.html]以获取更多信息。
Dhiraj Shrestha answered 2020-02-04T00:18:24Z
codeigniter mysql -1_mysql-Codeigniter-多个数据库连接相关推荐
- codeigniter mysql查询_php – CodeIgniter MySQL查询不起作用
我试图让这个查询在CodeIgniter中工作,但它正在吐出一个错误: A Database Error Occurred Error Number: 1096 No tables used SELE ...
- codeigniter mysql -1_CodeIgniter错误mysql
首先CodeIgniter连接数据库连不上,总是显示连接错误,但是又没有error信息,难以debug. 解决方案是:在application/config/database.php文件的最后加上这一 ...
- codeigniter mysql主键_codeigniter mysql视图与查询
我正在使用Codeigniter进行开发,并且当它涉及复杂的数据库查询时 我在用 $this->db->query('my complicated query'); 然后使用$query- ...
- codeigniter mysql -1_Codeigniter MySQL查询与变量
我正在尝试在Codeigniter中运行以下示例查询 SELECT users.id, users.first_name, users.last_name, users.game_id FROM us ...
- codeigniter mysql -1_在CodeIgniter中使用现有的MySQL数据库
在我的PHP网站中,我使用SiteTranslator脚本来翻译成30种语言的网站. 每个翻译都存储在自己的表中(text_en,text_de ...),每个表有3列(textKey,textVal ...
- codeigniter mysql error_CodeIgniter:无法使用提供的设置错误消息连接到数据库服务器...
我使用MySQL驱动程序就好了.我想改用MySQL驱动程序,但是只要我更改它(只需在MySQL末尾添加'i',并添加端口号)我就会收到以下错误消息 发生数据库错误 无法使用提供的设置连接到数据库服务器 ...
- codeigniter mysql 存储过程_Codeigniter框架使用Mysql存储过程的例子
执行存储过程 $query = $this -> db -> query('CALL YOU_SP_NAME'); $result = $query -> result(); 这个 ...
- 使用url连接mysql时的属性_MySQL数据库连接属性配置,即URL后一些配置参数及其重要性...
在JDBC中,Connection类的创建方式有三种函数(不包括连接池) 一是:DriverManager.getConnection(String url), 二是:DriverManager.ge ...
- mysql 连接openfire_修改openfire数据库连接(转)
初次安装openfire的时候设置的是采用内置的数据库hsqldb,需要修改为mysql数据库. 问题:在web的控制台上找不到修改数据库连接方式的修改. 解决: 重新设置数据连接先要在mysql数据 ...
- mysql连库串_数据库连接串整理 - osc_ac5z111b的个人空间 - OSCHINA - 中文开源技术交流社区...
常用JDBC驱动与连接字符串 MySQL driver:com.mysql.jdbc.Driver url:jdbc:mysql://localhost:3306/mydb MySQL url格式:j ...
最新文章
- OpenCV | OpenCV彩色图像直方图算法实现
- Android编译笔记二
- 数组按时间(字符串-Date)排序
- 深度学习(二)——深度学习常用术语解释, Neural Network Zoo, CNN, Autoencoder
- python之各种装饰器的使用
- [Leetcode][第733题][JAVA][图像渲染][BFS][DFS]
- 京东健康携手国控湖北 首批1500万只口罩专供湖北
- 给大家讲一个被社区团购小程序套路的经历吧
- 大数据面试都问些什么?
- mysql备份的三种方式
- ROS1节点,消息,话题,服务的介绍
- 【Spring-tx】AutoProxyRegistrar类
- Excel、Word VBA 学习笔记
- 测试测量(3)- 如何选择设备的平台
- 怎么查python题答案_超星尔雅Python语言应用查题教程
- WPS Office常用快捷键大全
- 【Redis】2. 入门篇
- python中encode函数_python中文处理之encode/decode函数
- 番茄花园域名转向Google
- three good things