<i id='p7yr8'><tr id='p7yr8'><dt id='p7yr8'><q id='p7yr8'><span id='p7yr8'><b id='p7yr8'><form id='p7yr8'><ins id='p7yr8'></ins><ul id='p7yr8'></ul><sub id='p7yr8'></sub></form><legend id='p7yr8'></legend><bdo id='p7yr8'><pre id='p7yr8'><center id='p7yr8'></center></pre></bdo></b><th id='p7yr8'></th></span></q></dt></tr></i><div id='p7yr8'><tfoot id='p7yr8'></tfoot><dl id='p7yr8'><fieldset id='p7yr8'></fieldset></dl></div>
  • <small id='p7yr8'></small><noframes id='p7yr8'>

    <tfoot id='p7yr8'></tfoot>

      <legend id='p7yr8'><style id='p7yr8'><dir id='p7yr8'><q id='p7yr8'></q></dir></style></legend>

        • <bdo id='p7yr8'></bdo><ul id='p7yr8'></ul>
      1. Yii2 创建数据库连接

        Yii2 Create Database Connection(Yii2 创建数据库连接)

        <small id='FwdID'></small><noframes id='FwdID'>

            <i id='FwdID'><tr id='FwdID'><dt id='FwdID'><q id='FwdID'><span id='FwdID'><b id='FwdID'><form id='FwdID'><ins id='FwdID'></ins><ul id='FwdID'></ul><sub id='FwdID'></sub></form><legend id='FwdID'></legend><bdo id='FwdID'><pre id='FwdID'><center id='FwdID'></center></pre></bdo></b><th id='FwdID'></th></span></q></dt></tr></i><div id='FwdID'><tfoot id='FwdID'></tfoot><dl id='FwdID'><fieldset id='FwdID'></fieldset></dl></div>

            <tfoot id='FwdID'></tfoot><legend id='FwdID'><style id='FwdID'><dir id='FwdID'><q id='FwdID'></q></dir></style></legend>

                <tbody id='FwdID'></tbody>
                <bdo id='FwdID'></bdo><ul id='FwdID'></ul>
                  本文介绍了Yii2 创建数据库连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想在不使用配置文件的情况下以编程方式创建数据库连接.我有一个表单,其中用户输入数据库详细信息,例如 hostnameusernamepassworddatabase name.根据这些细节,建立一个新的测试连接,如果通过,它应该生成必要的文件.这是我尝试过的:

                  I want to create a database connection programmatically without using the config file. I have a form wherein the user enters the database details like the hostname, username, password and the database name. On the basis of these details a new test connection is made and if it passes it should generate the necessary files. This is what I have tried:

                  // Create Test DB Connection
                  Yii::$app->set('id', [
                                      'class'    => 'yiidbConnection',
                                      'dsn'      => $dsn,
                                      'username' => $form->username,
                                      'password' => $form->password,
                                      'charset'  => 'utf8'
                                  ]);
                  try {
                      // Check DB Connection
                      if (Yii::$app->db->getIsActive()) {
                         // Write Config
                          $config['components']['db']['class']    = 'yiidbConnection';
                          $config['components']['db']['dsn']      = $dsn;
                          $config['components']['db']['username'] = $username;
                          $config['components']['db']['password'] = $password;
                          $config['components']['db']['charset']  = 'utf8';
                  
                          Configuration::setConfig($config);
                          $success = TRUE;
                          return $this->redirect(['init']);
                      }else{
                          $errorMsg = 'Incorrect Configurations';
                      }
                  } catch (Exception $e) {
                          $errorMsg = $e->getMessage();
                  }
                  

                  我一次又一次地对此进行了测试,即使配置正确,它也会出现错误.感谢所有的帮助.提前致谢!

                  I have tested this again and again and even with correct configurations it is giving an error. All the help is appreciated. Thanks in advance!

                  推荐答案

                  我意识到了我的错误.当使用 Yii::$app->set() 来设置 db 连接时,你甚至必须手动 open 使用Yii::$app->db->open().Yii 不会为您打开连接.

                  I realised my mistake. When using Yii::$app->set() for setting up the db connection, you have to even manually open the connection using Yii::$app->db->open(). Yii doesn't open up the connection for you.

                  这篇关于Yii2 创建数据库连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)

                  <small id='CHJd7'></small><noframes id='CHJd7'>

                    <bdo id='CHJd7'></bdo><ul id='CHJd7'></ul>
                    <legend id='CHJd7'><style id='CHJd7'><dir id='CHJd7'><q id='CHJd7'></q></dir></style></legend>
                      <tbody id='CHJd7'></tbody>
                    • <tfoot id='CHJd7'></tfoot>

                      1. <i id='CHJd7'><tr id='CHJd7'><dt id='CHJd7'><q id='CHJd7'><span id='CHJd7'><b id='CHJd7'><form id='CHJd7'><ins id='CHJd7'></ins><ul id='CHJd7'></ul><sub id='CHJd7'></sub></form><legend id='CHJd7'></legend><bdo id='CHJd7'><pre id='CHJd7'><center id='CHJd7'></center></pre></bdo></b><th id='CHJd7'></th></span></q></dt></tr></i><div id='CHJd7'><tfoot id='CHJd7'></tfoot><dl id='CHJd7'><fieldset id='CHJd7'></fieldset></dl></div>