Page 1 of 1

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

Posted: Fri May 21, 2010 6:40 am
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.

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

Posted: Fri May 21, 2010 7:02 am
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

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

Posted: Fri May 21, 2010 8:38 am
by Apollo
Yes, of course this is possible. Simply call mysql_connect twice, and use the appropriate connection resource when doing queries.

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

Posted: Sat May 22, 2010 2:01 am
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

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

Posted: Sat May 22, 2010 2:16 am
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.

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

Posted: Sat May 22, 2010 4:13 am
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

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

Posted: Sat May 22, 2010 8:22 am
by Apollo
Exactly what do you not understand?

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

Posted: Sat May 22, 2010 10:45 pm
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);