0

During development, we had to sign a view table of another database, but no matter how hard we tried, only an error occurred. So even though I just called it a simple select, an error occurs.

A Database Error Occurred

Error Number: 1046 No database selected

SELECT * 
FROM `test1`.`view_test_table`

Filename: test.php

I don't understand this situation.

I've done everything I've asked CodeIgniter document to do, and I've done everything from simple queries to query builder tests, but I can't see the details.

Test_mode.php is:

<?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();
    }
}

and 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
 );

Did I make any mistakes?

MGK
  • 3
  • 2
  • Have you read through the documentation? – Rotimi Dec 23 '18 at 17:15
  • Did you have a look at this? https://stackoverflow.com/a/8269596 – digijay Dec 23 '18 at 17:49
  • Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer Dec 23 '18 at 19:08
  • @halfer - I think there was a slight misunderstanding because it was used for the first time. I'll be careful from now on. – MGK Dec 24 '18 at 02:29
  • @Akintunde-Rotimi - https://www.codeigniter.com/userguide3/database/index.html – MGK Dec 24 '18 at 02:39
  • @digijay - thanks. but, I've already tried. – MGK Dec 24 '18 at 02:40

2 Answers2

1

Try using a new instance for this db

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

and refer tor the table view_test_table using new_db

ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
1

in your database.php file. write your database name.

$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
 );
PHP Geek
  • 3,949
  • 1
  • 16
  • 32
  • Thanks... it work!! I feel so empty. I spent a lot of time looking for it, but it was so simple. Still, I am very grateful for your help. but... Is that how it's perceived? Can't it work if I put it in the from syntax? – MGK Dec 24 '18 at 05:51