【问题标题】:CodeIgniter get view tables of another databaseCodeIgniter 获取另一个数据库的视图表
【发布时间】:2019-05-23 03:40:56
【问题描述】:

在开发过程中,我们不得不对另一个数据库的视图表进行签名,但无论我们多么努力,都只是出现了错误。因此,即使我只是将其称为简单选择,也会发生错误。

发生数据库错误

错误号:1046 未选择数据库

SELECT * 
FROM `test1`.`view_test_table`

文件名:test.php

我不明白这种情况。

我已经完成了我要求 CodeIgniter 文档做的所有事情,并且我已经完成了从简单查询到查询构建器测试的所有事情,但是我看不到详细信息。

Test_mode.php 是:

<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Test_model extends CI_Model
{
    private $new_db;

    function __construct()
    {
        parent::__construct();
        $this->new_db = $this->load->database('test1', TRUE);
    }

    public function test_view_list()
    {
       $this->new_db->select("*");
       $this->new_db->from("test1.view_test_table");
       return $this->new_db->get()->result_array();
    }
}

和database.php

$db['test1'] = array(
    'dsn'   => '',
    'hostname' => 'localhost',
    'username' => 'test1',
    'password' => 'xxxx',
    'database' => '',
    'dbdriver' => 'mysqli',
    'dbprefix' => '',
    'pconnect' => FALSE,
    '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
 );

我犯了什么错误吗?

【问题讨论】:

标签: php mysql codeigniter


【解决方案1】:

尝试为此数据库使用新实例

$new_db = $this->load->database('test1', TRUE);

并使用 new_db 引用表 view_test_table

【讨论】:

  • 非常感谢 :D,但我已经尝试过了。更新我的来源
【解决方案2】:

在您的 database.php 文件中。 写下你的数据库名称。

$db['test1'] = array(
    'dsn'   => '',
    'hostname' => 'localhost',
    'username' => 'test1',
    'password' => 'xxxx',
    'database' => 'databasename',
    'dbdriver' => 'mysqli',
    'dbprefix' => '',
    'pconnect' => FALSE,
    '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
 );

【讨论】:

  • 谢谢...它工作!我觉得好空虚。我花了很多时间寻找它,但它是如此简单。不过,我非常感谢您的帮助。但是……这就是人们对它的看法吗?如果我把它放在 from 语法中,它不能工作吗?
猜你喜欢
  • 2022-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多