NYCPHP Meetup

NYPHP.org

[nycphp-talk] different sites, one database

Alberto acas at sapo.pt
Wed Jul 13 11:43:51 EDT 2005


On Wed, 2005-07-13 at 12:15 -0300, Henry Ponce wrote:

> Hello all:
> 
> I've been trying to solve this on my own with no luck. I can't seem to find 
> the answer on google. I get different problems/solutions but not the answer 
> to what I want to do. Maybe someone here can help me or point me in the right 
> direction.

That's what we're here for.

> 
> Here's what I need to do... 
> 
> I have a dedicated server with different sites hosted, each with their own 
> mysql database which I connect to perfectly in my php scripts, using 
> "localhost" as the server in the mysql_connect() function.
> 
> The sites are for example...site1.com, site2.com, site3.com.
> 
> Well, now I need to connect to a mysql database on site1.com that holds 
> information that is usefull and needed for most sites. I need to do this on 
> scripts that run on site2.com and site3.com.
> 
> How can I connect to the mysql database on site1.com, when I am running a 
> script on site2.com or site3.com?
> 
> I can't use "localhost" as the server in the mysql_connect() function in the 
> scripts on site2.com or site3.com, because I would be connecting to the mysql 
> database, on each site, which I understand. 

Easy.
First, I assume you have configured MySQL to allow different users to
each database, right?
If not so, please do, either by using a graphical tool like phpMyAdmin,
or by hand, see the MySQL Manual.
Second, maybe first, nevermind that, you have ONE machine with all the
sites inside, right?

Having checked this, you WILL connect to server "localhost" because it
is the only server you have, no matter how many databases it serves.

Now, on your scripts, if you do not use a DBAL (database abstraction
layer) in which case you should consider either using an existing one,
or writting one (as I do), you  only have to invoque the database with
mysql_connect:

mysql_connect("localhost",USER,PASSWORD); // USER and PASSWORD MUST be
the ones allowed to access the database you want.
mysql_select_db(DATABASE);

And Query it...

With this you should be on your way.



> 
> Help!!
> 
> Thank you in advance and I hope my explanation was clear.
> 
> Henry


You're welcome.


Alberto dos Santos
email:     acas at sapo.pt
skype:    fatflash
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.nyphp.org/pipermail/talk/attachments/20050713/d61fe227/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
URL: <http://lists.nyphp.org/pipermail/talk/attachments/20050713/d61fe227/attachment.sig>


More information about the talk mailing list