SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Connecting to Remote/Multiple Databases

    Given a database connection that looks like this...

    PHP Code:
    @mysql_connect ("localhost" "star_dust" "supernova");
    @
    mysql_select_db ("star_light"); 
    how would you change it if your database was on a different website, say http://www.othersite.org/ Let's say I can't have a database on http://www.newsite.org/, so I want my database connection to point to www.othersite.org.

    And what would the query look like if you wanted to access TWO databases, one localhost and the other on www.othersite.org?

    Thanks.

  2. #2
    SitePoint Member
    Join Date
    Mar 2004
    Location
    Oslo, Norway
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just change the localhost setting to whatever server your database is located on, eg www.othersite.org.

    As far as I know, you can't access to different databases within the same query. But if you'd like to have to different connections open at the same time, you could use code like this:

    PHP Code:

    # Connecting and selecting local database
    $dbLocal = @mysql_connect ("localhost" "star_dust" "supernova");
    @
    mysql_select_db ("star_light"$dbLocal); 

    # Connecting and selecting external database
    $dbExternal = @mysql_connect ("www.othersite.org" "star_dust" "supernova");
    @
    mysql_select_db ("star_light"$dbExternal); 

    # Doing a query on the local database
    $res1 mysql_query("SELECT ..."$dbLocal);

    # Doing a query on the external database;
    $res2 mysql_query("SELECT ..."$dbExternal); 

  3. #3
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmmmm... I tried a couple variations...

    $dbExternal = @mysql_connect ("www.othersite.com" , "USERNAME" , "PASSWORD");
    @mysql_select_db ("DATABASE", $dbExternal);

    and

    @mysql_connect ("www.othersite.com" , "USERNAME" , "PASSWORD");
    @mysql_select_db ("DATABASE");

    but neither one seems to work. They both say "Invalid Query: No database selected" at the bottom of the page.

    I also get error messages similar to these:

    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/USERNAME/public_html/connection.php on line 22

    Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/USERNAME?/public_html/connection.php on line 40

    Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/USERNAME?/public_html/connection.php on line 44

    * * * * * * * * * *

    Doesn't it look like it's referencing a page outside my website - outside /public_html/?

    Line 22 is the second line in the query below:

    $result = mysql_query($qry);
    $type = mysql_fetch_array($result);
    $mytype = "my" . $type['AreaType'];
    $$mytype = $type['Name'];

    Lines 40 and 44 are the first and last lines in the query below:

    $type = mysql_fetch_assoc($result);
    $mytype = "my" . $type['AreaType'];
    $data = array();
    $data_details = mysql_fetch_assoc(mysql_query("SELECT *
    FROM basics WHERE IDArea LIKE '$mycode'"));

    I discovered that my database was empty - my attempt to import tables failed. However, these error messages suggest the database connection itself is bad. I now have a table online, but I get the same error message. Any tips?

    Thanks.

    Quote Originally Posted by aQustic
    Just change the localhost setting to whatever server your database is located on, eg www.othersite.org.

    As far as I know, you can't access to different databases within the same query. But if you'd like to have to different connections open at the same time, you could use code like this:

    PHP Code:

    # Connecting and selecting local database
    $dbLocal = @mysql_connect ("localhost" "star_dust" "supernova");
    @
    mysql_select_db ("star_light"$dbLocal); 

    # Connecting and selecting external database
    $dbExternal = @mysql_connect ("www.othersite.org" "star_dust" "supernova");
    @
    mysql_select_db ("star_light"$dbExternal); 

    # Doing a query on the local database
    $res1 mysql_query("SELECT ..."$dbLocal);

    # Doing a query on the external database;
    $res2 mysql_query("SELECT ..."$dbExternal); 

  4. #4
    SitePoint Enthusiast ModestITExpert's Avatar
    Join Date
    Jun 2004
    Location
    Planet Earth
    Posts
    73
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello,

    The first thing you need to do is to check if your remote server allows connections tp mysql from the remote host (not localhost)! Almost all shared hosting providers bans this beacuse of the security issue.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •