Can We connect to both locally & to Server at a time ??

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
amitdubey2
Forum Commoner
Posts: 48
Joined: Tue Apr 13, 2010 10:13 pm

Can We connect to both locally & to Server at a time ??

Post by amitdubey2 »

Hi to all,

Here i got a dilemma, when i am working on a project.
Is it possible to connect localhost and to server with the same script at a time in php.
Actually my client need a application in which when he locally operates on the software ...it must be updated in the serverside..without updating it to server.it just update the things which are updated on the local machine.
I dont know exactly is it fisible or not ...(i knw that it is not possible to make a local database available to server for updating but the client is saying that he has seen this type of application somewhere..and the confidence he was showing is forcing me to have a look for this concept,i got some code in which a function decides the connect with the localhost or server but i am sure that is not the thing for which i am searching ) :?: :?: :?:

Any help regarding this will be wonderful. :idea: :idea:
Thanks




Amit.
Last edited by amitdubey2 on Fri May 21, 2010 8:00 am, edited 2 times in total.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Can We connect to both locally & to Server at a time ??

Post by Benjamin »

Forum Rules wrote: 11. Please use proper, complete spelling when posting in the forums. AOL Speak, leet speak and other abbreviated wording can confuse those that are trying to help you (or those that you are trying to help). Please keep in mind that there are many people from many countries that use our forums to read, post and learn. They do not always speak English as well as some of us, nor do they know these aberrant abbreviations. Therefore, use as few abbreviations as possible, especially when using such simple words.
You may also want to read:
  1. General Posting Guidelines
  2. Posting Code in the Forums
  3. PHP Manual
  4. PHP Tutorials
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: Can We connect to both locally & to Server at a time ??

Post by Apollo »

Yes, of course this is possible. Simply call mysql_connect twice, and use the appropriate connection resource when doing queries.
amitdubey2
Forum Commoner
Posts: 48
Joined: Tue Apr 13, 2010 10:13 pm

Re: Can We connect to both locally & to Server at a time ??

Post by amitdubey2 »

hello Apollo,
by Apollo » Fri May 21, 2010 8:08 pm

Yes, of course this is possible. Simply call mysql_connect twice, and use the appropriate connection resource when doing queries.
As you said above, i think you are talking about choosing a connection either localhost or server in the same script ,Is it so???
Or can you please give me a example for that?


I think you didn,t get my situation correctly ...
The situation is like when my client is working on localhost and modified something or updated something...in the local-machine's database...and if that time internet connection is not there ...
At the moment when local machine will get some connection ...it must be automatically update all the database on the server side.. how it is possible.. :dubious: :?:

Some more information with example would be good. :)
thank you.



regards
amit
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: Can We connect to both locally & to Server at a time ??

Post by Apollo »

Well in that case I would say...

1. try to make a connection to the remote database
2. make a connection to the local database
3. if 1. succeeded, get everything from the local database (i.e. from connection 2) and store it in the remote database (i.e. to connection 1), and close connection 2. Use connection 1 for the rest of the session or script execution time.
4. if 1. failed, use connection 2 for the rest of the session or script (thus storing everything local). If you use the same procedure next time, everything will eventually get stored remotely.
amitdubey2
Forum Commoner
Posts: 48
Joined: Tue Apr 13, 2010 10:13 pm

Re: Can We connect to both locally & to Server at a time ??

Post by amitdubey2 »

hello apolo,

What you said above it seems working ...but i am unable to get logic totally..
so can you be more clear regarding this by giving some example or reference.So that i can proceede further... :dubious:
And Thanks for your concern.





regards
amit
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: Can We connect to both locally & to Server at a time ??

Post by Apollo »

Exactly what do you not understand?
User avatar
Chalks
Forum Contributor
Posts: 447
Joined: Thu Jul 12, 2007 7:55 am
Location: Indiana

Re: Can We connect to both locally & to Server at a time ??

Post by Chalks »

Apollo wrote:Exactly what do you not understand?
Sounds like all of it. >.>

Apollo wrote:Well in that case I would say...

1. try to make a connection to the remote database
2. make a connection to the local database
3. if 1. succeeded, get everything from the local database (i.e. from connection 2) and store it in the remote database (i.e. to connection 1), and close connection 2. Use connection 1 for the rest of the session or script execution time.
4. if 1. failed, use connection 2 for the rest of the session or script (thus storing everything local). If you use the same procedure next time, everything will eventually get stored remotely.

Code: Select all

// 1.
$remote_db = mysql_connect("remote_host_location", "username", "password");

// 2.
$local_db = mysql_connect("localhost", "username", "password");

// 3.
if($remote_db) {
  mysql_select_db("database", $remote_db);
  mysql_close($local_db);
  $will_close = $remote_db;
}

// 4.
else {
  mysql_select_db("database", $local_db);
  $will_close = $local_db;
}

// do desired queries here

// close database here
mysql_close($will_close);
Post Reply