473,406 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

how to connect 2 databases simlutaneously using php

245 100+
Hey,
I want to connect to 2 different MYSQL databases at the same time. Does somebody help me out that how can i do that closing one connection and selecting other and vice versa...?


kind regards,
Mohsin Rafique
Jan 20 '10 #1

✓ answered by Atli

Hey.

If you are using the old MySQL extension, then all you would have to do is retrieve the return value of the mysql_connect function and use that value with the other mysql_ functions.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $db1 = mysql_connect('host1', 'usr', 'pwd');
  3. $db2 = mysql_connect('host2', 'usr', 'pwd');
  4.  
  5. mysql_select_db('dbName', $db1);
  6. mysql_select_db('dbName', $db2);
  7.  
  8. mysql_query($sql, $db1);
  9. mysql_query($sql, $db2);
  10. ?>

2 2328
Atli
5,058 Expert 4TB
Hey.

If you are using the old MySQL extension, then all you would have to do is retrieve the return value of the mysql_connect function and use that value with the other mysql_ functions.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $db1 = mysql_connect('host1', 'usr', 'pwd');
  3. $db2 = mysql_connect('host2', 'usr', 'pwd');
  4.  
  5. mysql_select_db('dbName', $db1);
  6. mysql_select_db('dbName', $db2);
  7.  
  8. mysql_query($sql, $db1);
  9. mysql_query($sql, $db2);
  10. ?>
Jan 20 '10 #2
dlite922
1,584 Expert 1GB
Then one might ask: what if you want to join two tables from two databases together?

easy, MySQL query allows you to select which database a table is from by appending the database number, a dot, and then the table name like so: db1.my_table JOIN db2.second_table.


Food for thought,



Dan
Jan 20 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

20
by: Mr Dygi | last post by:
Hi, PHP 4.3.4 installed manually from package *.zip and Apache 2. I have a problem with this simple code: <?php $link = mysql_connect("127.0.0.1","","") or die("Could not connect: " ....
3
by: Dad | last post by:
I need to connect SQL Server 2000 to DB2 on z/OS through DB2 Connect 8. I can successfully connect and query data through a System DSN, but trying to link the server using this DSN and MSDASQL...
6
by: AP | last post by:
I have not seen any good examples on how to connect to a mySQL db. My website uses phpMyAdmin to administer the SQL databases. What confuses me is where are the databases that I create located? For...
6
by: yoyo | last post by:
Mabey I'm missing something very basic, but I can't seem to get it to work. I'm trying to follow along in the sample programs that tells how to do that, but mine still doesn't work. In my...
4
by: Frank | last post by:
Hi I am a poor student studying towards my MCSD. I have managed to get a cheapish student version of vb.net. Problem is that this version does not connect to sql databases and I need it to...
1
by: Tom Andersen | last post by:
Hi all Im trying to install DB2 Connect from a central installation point. and the program installs as it should, on the target machine, Is there a way to implement the databases into the...
4
by: traceable1 | last post by:
I have a couple new servers - Windows 2003 R2 - with SQL Server 2000 SP4 installed. They are all Active/Active clustered instances (my first ones). I am trying to create database links to other...
3
by: cberthu | last post by:
Hi all, Is it possible to have two connects in the same rexx script to different DB's? I have to get data form on DB (with specifics selects and filter out some values with RExx) and save the...
5
by: Alan Silver | last post by:
Hello, Server configuration: Windows 2003 Server SP2 SQL Server 2000 SP4 ..NET v2.0.50727 just built up a new server using the same configuration as my current one. I even used the same CDs...
7
by: sanjay | last post by:
Hi to all the people. please will u tell how can i Connect to the Database through C program. i have No clarity in that.
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.