I have 2 databases on a hosting, with one running a site (BD1), into the other some data (BD2) is being uploaded, which needs to be updated on the site. How do I connect to (BD2) and get data from there? Site on bitrix, I try it like this, but everything is empty, that is, it does not connect to the base. Maybe using php, without the Bitrix API, can this be done somehow?

<?require($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_before.php");?> <? $DBHost = "localhost"; $DBName = "111"; $DBLogin = "111"; $DBPassword = "111"; if(!($DB->Connect($DBHost, $DBName, $DBLogin, $DBPassword))) { $results = $DB->Query("SELECT `name` FROM `cities`"); var_dump($results); } ?> 

    2 answers 2

    I can not agree with the decision. Simply create a new instance of the CDatabase object.

     <?php require($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_before.php");?> <?php $DBnew = new CDatabase(); $DBHost = "localhost"; $DBName = "111"; $DBLogin = "111"; $DBPassword = "111"; if(!($DBnew->Connect($DBHost, $DBName, $DBLogin, $DBPassword))) { $results = $DBnew->Query("SELECT `name` FROM `cities`"); var_dump($results); } ?> 

    Thus, firstly, statistics on SQL queries will be collected, and secondly, you will not have to keep in mind the peculiarities of calls with mysql_query , which, moreover, "has become outdated since PHP 5.5.0 and will be deleted in the future . "

      Here is an example of connecting two databases:

       $dbOne = new mysqli($host, $user, $password, $db1); $dbTwo = new mysqli($host, $user, $password, $db2); 

      Here is an example of a sample from the first database:

       $dbOne->query("select * from tablename"); 

      Here is an example of a sample from the second database:

       $dbTwo->query("select * from tablename"); 

      Successful coding ...

      • Thanks, it happened so, through Bitrix, it seems to give you a job with only one DB - Nikolay
      • This is a solution that will stop working with one of the future php updates ( php.net/manual/ru/function.mysql-query.php ). - Andrewus
      • The mysql_ * functions are deprecated. Using them in the "new code" is a bad form. - newman
      • Thanks for the comments. Updated. - Urmuz Tagizade