[PHP/MSSQL] Working between 2 databases

⚠️
Self Deleted
Joined
Sep 8, 2007
Messages
202
Reaction score
24
I need to Select data from 1 Database.Table and update another Database.Table.

PHP:
<!-- Current DB Connect/Select -->
$connect = mssql_connect("$Server", "$User_ID", "$Password");
$member_db = mssql_select_db("$Member"); <!-- only 1 database -->
----------------
<!-- Queries i need to execute -->
<!-- Database 1 Query -->
mssql_query("SELECT column_name1 FROM table WHERE column_name2 = 'Value1'");
<!-- Database 2 Query -->
mssql_query("UPDATE table SET coumn_name3 = 'value3'  WHERE column_name1 = 'value2'");
Any help help would be greatly appreciated.

Sorry i didn't' really know how to explain the problem.


Thanks for your time and patience

Sincerely,
- xHalloweenx
 
You can use ".." to specify the database on the left hand side and table on the right.

Code:
mssql_query("SELECT column_name1 FROM database1..table WHERE column_name2 = 'Value1'");
<!-- Database 2 Query -->
mssql_query("UPDATE database2..table SET coumn_name3 = 'value3'  WHERE column_name1 = 'value2'");
 
A single dot suffices, and it is quite possible to select from several databases in the same query, provided your login credentials are valid for both databases:

PHP:
UPDATE    `secondary`.`table`
  SET     `column_3`  = 
          (SELECT   `column_1`
           FROM     `primary`.`table`
           WHERE    `column_2`  = 'value_1')
  WHERE   `column_1`  = 'value_2'

Mind the layout and the accents: they increase readability and reduce the chance of accidentally selecting from the wrong table. An SQL query should never be written on a single line.
 
Back