Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ErrorException when Connecting to Multiple Databases #255

Closed
mithazhe opened this issue Sep 9, 2016 · 6 comments
Closed

ErrorException when Connecting to Multiple Databases #255

mithazhe opened this issue Sep 9, 2016 · 6 comments

Comments

@mithazhe
Copy link
Contributor

mithazhe commented Sep 9, 2016

I have two databases, and try to make multiple connections like this:
$blogDbUsers = \Config\Database::connect('users');
$blogDbBlogs = \Config\Database::connect('blogs');

Then, I got problem like this:
ErrorException
Trying to get property of non-object
BASEPATH/Database\MySQLi\Connection.php at line 280

@lonnieezell
Copy link
Member

Just to confirm: users and blogs are separate databases, not tables, right?

It sounds like they would be tables. In which case, you only need the one connection.

@mithazhe
Copy link
Contributor Author

mithazhe commented Sep 9, 2016

Yes, they are separate databases.
I know they look like tables, but they are two different databases.

@lonnieezell
Copy link
Member

Please ensure that both of your database connections are setup properly in Config\Database. I just tried locally and it worked.

$db1 = \Config\Database::connect('default');
$db2 = \Config\Database::connect('tests');

d($db1->listTables());
d($db2->listTables());

@mithazhe
Copy link
Contributor Author

mithazhe commented Sep 9, 2016

That's works. I tested it. However I got that error when I load the view.
I can't see the the error details on that page, but I can see in view source like this

I see the backtrace:
ErrorException
Trying to get property of non-object
BASEPATH/Database\MySQLi\Connection.php at line 280

BASEPATH/Debug\Toolbar\Collectors\Database.php : 152
On method CodeIgniter\Database\MySQLi\Connection->getVersion

BASEPATH/Debug\Toolbar\View\toolbar.tpl.php : 49
On method CodeIgniter\Debug\Toolbar\Collectors\Database->display

APPPATH/Filters\DebugToolbar.php : 47
On method CodeIgniter\Debug\Toolbar->run

The problem is on that lines I guest.

@lonnieezell
Copy link
Member

Thanks for pointing me in the right direction. The error happened because no queries had been run so far, so the database connection had not actually been initialized. So, it couldn't grab the version of the database when trying to display it in the toolbar. Fix to follow.

@mithazhe
Copy link
Contributor Author

You're welcome :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants