E
E
Evgeny Ivanov2018-02-17 11:49:14
PHP
Evgeny Ivanov, 2018-02-17 11:49:14

How to work with two MySQl databases?

Always worked with one DB (MySQL). Now it was required to make a selection from a table in one database, and then from a table in another. What is the best way to organize it?
Now, in each file where there are requests, the code from the mysql.php file is "included" at the beginning

$hostname='localhost';
$username='root';
$password='';
$basename='base1';
mysql_connect($hostname, $username, $password) or die (mysql_error());
mysql_select_db($basename) or die (mysql_error());
mysql_query("set character_set_client	='utf8'");
mysql_query("set character_set_results	='utf8'");
mysql_query("set collation_connection	='utf8_general_ci'");

But now I have 2 bases. Surely it should?
$hostname='localhost';
$username='root';
$password='';
mysql_connect($hostname, $username, $password) or die (mysql_error());
//вызов mysql_select_db в этом месте важен?
mysql_query("set character_set_client='utf8'"); 
mysql_query("set character_set_results='utf8'");
mysql_query("set collation_connection='utf8_general_ci'");


mysql_select_db('base1') or die (mysql_error());
//запросы к этой базе

mysql_select_db('base2') or die (mysql_error());
//запросы к этой базе

How to work with two MySQl databases?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Max Sh, 2018-02-17
@maxshabl

Probably yes))) Works? I would make two connections, it's more convenient, or select the base before each request. As I understand it, the task is educational, so any option is acceptable.
In real projects, they make different connections and use OOP.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question