Jump to content

[SOLVED] mysql_connect() connecting to database on another server


sd9sd

Recommended Posts

Done a lot of googling but the solutions didn't work. Thought I'd post it in a way which would help others as well as me.

I'm using fictitious passwords and domain names, so let these not be confused with any constants.

 

I'm working on a website called www.something.com

My SQL database is on a domain called www.nothing.com

I've logged into www.nothing.com with the username 'sam' and password 'abc'.

I created a database named 'icecream'. In this database I created a table named 'tags'.

 

I've got a PHP file which I'm running from www.something.com as http://www.something.com/testing.php

 

This is the code I'm using to connect to the icecream database.

$host="ftp.nothing.com";
$user="sam";
$password="abc";

$conn = @ftp_connect("$host");
if (!$conn) {echo("&result=0&");echo 'Error: Could not connect to ftp server';ftp_close($conn);exit;}
$result = @ftp_login($conn, $user, $password);
if (!$result) {echo("&result=0&");echo "Error: Could not log on as $user";ftp_quit($conn);exit;}

$DB_SERVER="www.nothing.com";
$DB_USER="sam_owner";
$DB_PASS="abc";
$DB_NAME="sam_tags";
define("TBL_USERS", "icecream");

$connection = @mysql_connect($DB_SERVER, $DB_USER, $DB_PASS);
if (!$connection) {echo "SQL error. Tag names couldn't be obtained";}
$sc = @mysql_select_db($DB_NAME, $connection);

 

I'm able to login to the nothing.com website but can't connect to the database. Could anyone be kind enough to tell me where I've gone wrong?

Link to comment
Share on other sites

what error messages are you getting... I have connected to a remote database like this before and it worked fine but i did use the ip address.

 

If you are running this script on your website www.something.com you do not need the ftp section at the top to connect to www.nothing.com or is this just a test for the username and password?

 

Make sure that the username and password for the DB are correct. As i understand it you are using the same username and password for the ftp to this server as for the database..

 

Hope this helps

Link to comment
Share on other sites

Thanks lamez and DarkWater....I had an email conversation with the domain host (nettigritty). They say that they've disabled remote SQL access for the past five years due to security reasons. It's necessary for me to access it remotely coz both sites access the same database. I've been thinking of alternate options and one option seems to be to have a php file in www.nothing.com, which access the database and sends the info to the php file which calls it. The calling php will be in www.something.com.

 

@Bendude: the error was suppressed with the @, but the main point is that I've used the exact same code for connecting to the database using localhost (while I was working on www.nothing.com). So the names are correct...I'm sure of it.

Thanks for mentioning that logging in to the website isn't necessary...that helped.

 

 

Link to comment
Share on other sites

yes i can see that you were supressing the error messages but i just thought that might help locate the problem..

 

if you retrieved the information with a php file on the same server as the database you could include that php file onto your other server using server side include. I think some hosting companies disable including remote files also though for security

 

 

Link to comment
Share on other sites

Thanks Bendude14....I tried including it like this...

This is the caller.php file on www.something.com

<?php
include("http://www.nothing.com/called.php");
$id=$_POST["id"];
echo "id value is =$id";
$id=$_GET["id"];//so that it'd receive the value either by get or post
echo "id value is =$id";
?>

and this is the code in called.php

<?php
echo "yay";
?>

But it gives an error saying:

Warning: include() [function.include]: URL file-access is disabled in the server configuration in /home/LOGINNAME/public_html/caller.php on line 2

 

Warning: include(http://www.nothing.com/called.php) [function.include]: failed to open stream: no suitable wrapper could be found in /home/LOGINNAME/public_html/caller.php on line 2

 

Warning: include() [function.include]: Failed opening 'http://www.nothing.com/called.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/LOGINNAME/public_html/caller.php on line 2

id value is =id value is =

 

I'm still only starting to understand how to use php code, so please bear with me...how is this include supposed to be done?

Link to comment
Share on other sites

Darn! isn't there ANY way to access the database then?

I hoped that php would have a function like send_query("www.nothing.com/called.php?id=1");

But they haven't provided any such function either.

 

Any ideas please? I need both websites to access the same database.

Link to comment
Share on other sites

I dont no of any other way to do it. Maybe you could get a third server just for the database that allows remote sql logins and they could both connect to it that way.

 

Someone else might come up with another idea though.

 

Link to comment
Share on other sites

if your host provider doesn't allow remote access, one option is to move the remote domain onto the same server as the local domain. This way, you'll be able to access the database from both sites as localhost.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.