0

I have problem with 2 select Databases from the same connection simultaneously, the code is:

@$dbmssSQLGestasa_conn = mssql_connect($servidor, $usuario, $contra);
if(!$dbmssSQLGestasa_conn){
        header("Location:nobase.php");
        exit();
     }

mssql_select_db('GESTASA', $dbmssSQLGestasa_conn);

the code of the other connection:

@$dbmssSQLTasa_conn = mssql_connect($servidor, $usuario, $contra);
     if(!$dbmssSQLTasa_conn){
        header("Location:nobase.php");
        exit();
     }

//Apertura de la base de datos
mssql_select_db('TASA', $dbmssSQLTasa_conn);

it dont work and give me error "(severity 16)".

is It possible do 2 or more selections databases en la same connection mssql?

Ali Gonzalo
  • 43
  • 1
  • 5
  • Possible duplicate of [How to connect to 2 databases at the same time in PHP](http://stackoverflow.com/questions/235264/how-to-connect-to-2-databases-at-the-same-time-in-php) – Vineet1982 Dec 11 '15 at 12:05

2 Answers2

0

Dont use mysql_select_db();

In Mysqli :

$servername = "localhost";
$username = "username";
$password = "password";

// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
     die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT * FROM test123.posts";  // databse.tablename
$result = $conn->query($sql);

if ($result->num_rows > 0) {
     // output data of each row
     while($row = $result->fetch_assoc()) {
            $mack[] = $row;     
     }
}

$sql1 = "SELECT * FROM portal.abouts";  // databse.tablename
$result1 = $conn->query($sql1);

if ($result1->num_rows > 0) {
     // output data of each row
     while($row1 = $result1->fetch_assoc()) {
            $mack1[] = $row1;     
     }
}
echo "<pre>";
print_r($mack);
print_r($mack1);
echo "</pre>";
$conn->close();
Monty
  • 1,110
  • 7
  • 15
0

You can easily use 2 databases in same time with Below Codes I'm Using variables as Capital word But you can use your own variable words.

<?php
  define('HOST', "YOURHOSTNAME");
  define('USER', "YOURHOSTNAME");
  define('PASS', "YOURHOSTNAME");
  define('DATABASE1', "NAMEOFDATABASE1");
  define('DATABASE2', "NAMEOFDATABASE2");

  $DATABASE1  = mysqli_connect(HOST, USER, PASS, DATABASE1);
  $DATABASE2  = mysqli_connect(HOST, USER, PASS, DATABASE2);
  if(!$DATABASE1){
      die("DATABASE1 CONNECTION ERROR: ".mysqli_connect_error());
   }
  if(!$DATABASE2){
      die("DATABASE2 CONNECTION ERROR: ".mysqli_connect_error());
   }


   $sql = "SELECT * FROM TABLE";   /* You can use your own query */

   $DATABASE1_QUERY = mysqli_query($DATABASE1, $sql);
   $DATABASE2_QUERY = mysqli_query($DATABASE2, $sql);

   $DATABASE1_RESULT = mysqli_fetch_assoc($DATABASE1_QUERY);
   $DATABASE2_RESULT = mysqli_fetch_assoc($DATABASE2_QUERY);

   /* SHOW YOUR RESULT HERE WHICH DATABASE YOU WANT FROM */
   echo  $DATABASE1_RESULT['id'];
   echo  $DATABASE2_RESULT['id'];


  /*After complete your all work don't forgot about close database connections*/
  mysqli_close($DATABASE1);
  mysqli_close($DATABASE2);
      ?>
Rajpal Singh
  • 307
  • 1
  • 12
  • this works with `ms sql server 2008` which is what was asked for? Also, the question asked about two databases from one connection. Which is possible in mysqli but this isn't what your answer shows. It is a useful answer but maybe not here? :) – Ryan Vincent Aug 06 '16 at 10:49
  • @RyanVincent - Okay! by some reading mistakes I give this answer . – Rajpal Singh Aug 06 '16 at 10:52