1

Wondering if it is possible to include two mysqli connection include php files on the same page.

I have a basic site I am using to learn/develop in php with mysql. I have one database for storing user information and another for website specific information. I can use both independently from each other without any issues.

Config.php:

DEFINE("HOST", "127.0.0.1");
DEFINE("USER","root");
DEFINE("PASS","");
DEFINE("DB","sv");

connect.php:

include_once 'config.php'; 
$mysqli = new mysqli(HOST, USER, PASS, DB);

sec_config.php:

define("HOST", "127.0.0.1");    
define("USER", "sec_user");    
define("PASSWORD", "");  
define("DATABASE", "svn");    

sec_connect.php:

include_once 'sec_config.php';   // As functions.php is not included
$mysqli = new mysqli(HOST, USER, PASSWORD, DATABASE);

My php pages can successfully query and display data from these connections. My index.php currently contained included the sec_connect, I am trying to add another php page which (within itself) includes

include_once("connect.php");

When I try and run the page I receive

mysqli::mysqli(): php_network_getaddresses: getaddrinfo failed: No such host is known.
mysqli::mysqli(): (HY000/2002): php_network_getaddresses: ...as above
mysqli::prepare(): Couldn't fetch mysqli in
Fatal error: Call to a member function bind_param() on a non-object in

I have simply commented out the above include_once to get everything working again.

I am using windows with xamp for dev, I have tried accessing via localhost and my ip address and I receive the same error. I have tried editing the hosts file and I have also tried amending the values in the connect files to be unique (changed back as it didnt help).

Hopefully this is the righ question to ask: Is it possible to include two mysqli database connections via different includes from my index.php page?

n34_panda
  • 2,577
  • 5
  • 24
  • 40
  • This might help you out... http://stackoverflow.com/questions/274892/how-do-you-connect-to-multiple-mysql-databases-on-a-single-webpage – Paul Dessert Jun 19 '14 at 22:18
  • Thanks, a bit more googling led me to this which looks very similar but displays the mysqli syntax: http://schoolsofweb.com/how-to-connect-to-multiple-mysql-databases-on-a-single-web-page-in-php/ – n34_panda Jun 19 '14 at 22:22

1 Answers1

3

This should work, provided you refer to the two different connections with different variables (they are two distinct instances after all).

For example:

file1.php

$db1 = new mysqli(HOST1, USER1, PASSWORD1, DATABASE1);

file2.php

$db2 = new mysqli(HOST2, USER2, PASSWORD2, DATABASE2);

file3.php

require "file1.php"
require "file2.php"

$result1 = $db1->query("SELECT * FROM table");

$result2 = $db2->query("SELECT * FROM some_other_table");
Kryten
  • 15,230
  • 6
  • 45
  • 68
  • I assume it isn't a problem to have file 3 broken into file3 (for $DB1 queries) and file4 (for $DB2 queries) and then display results from both on file5 (index.php) via include_once file3/4 - does that make sense? Trying it now – n34_panda Jun 19 '14 at 22:27
  • I am still struggling with this - I've done the above but it still produces the same as soon as I include my 2nd db query.php file on the index.php (along side the existing sec one) – n34_panda Jun 20 '14 at 17:28