Database connection from one server to another server - php

Is it possible to connect to a database lying on different server through another server ?
For example :-
I want to access the users table of www.abc.com from www.xqz.com, both are on different server.
Can you give me any ideas ?
will really appreciate if provide me the php query to connect.
Thanks.

"Is it possible" is a bit over-reaching, I think. To give you the shortest answer, yes, it is possible. However, whether or not you could do it depends on a lot of variables, not least of which is whether your hosting providers allow it. For instance, I use Hostmonster for Linux hosting, and to remotely access my MySQL database I have to first whitelist the IP address of the machine that will be accessing it.
Most hosting services provide a F.A.Q. section that should do a decent job of answering this particular question for you. Some will even provide support technicians help you set it up (Hostmonster does, within reason).
HTH.

Try This :
<?php
// Server in the this format: <computer>\<instance name> or
// <server>,<port> when using a non default port number
// Suppose your "www.abc.com" has an IP = 192.168.1.1
// So to connect, do something like this :
$server = '192.168.1.1,PORT_NUMBER'; // FOR WINDOWS
$server = '192.168.1.1:PORT_NUMBER'; // FOR LINUX
// Connect to MSSQL
$link = mssql_connect($server, [username], [password]);
if (!$link) {
die('Something went wrong while connecting to MSSQL');
}
?>

Related

PHP & PDO: Connect to MySQL using IPv6 address

I want to connect to a remote MySQL instance (a Google Cloud SQL one) by using its IPv6 address.
I'm using PHP PDO like that:
$db = new \PDO('mysql:host=<ipv6-address>;port=3306;dbname=<database-name>',
'<username>',
'<password>'
);
But it always fails with the following exception message:
PDOException: SQLSTATE[HY000] [2002] No route to host
From the terminal I can connect to the MySQL instance, without any issue, like this:
mysql --host=<ipv6-address> --user=<username> --<password>
Any help will be really appreciated.
Thanks
In case anyone else stumbles over the same problem, and to save them 2 hours delving through PHP source, PDO MySQL IPv6 connections work if you put square brackets around the address.
See: https://github.com/php/php-src/blob/master/main/streams/xp_socket.c#L568
e.g.
$pdo = new PDO("mysql:host=[1234:5678::42];port=3306;dbname=foo", ...);
Reading this https://www.saotn.org/php-mysql-and-ipv6-still-slow/ gives the following idea:
Knowing that, normally, IPv6 takes precedence over IPv4 (which is configurable), users are left with a slow responding website and database operations, only because connecting to an IPv6 address in PHP is refused, and the connection refused isn’t handled correctly, making the fallback to IPv4 slow. It takes mysql.connect_timeout seconds
Note: the source does seem credible
Also, this is a good read: http://dev.mysql.com/worklog/task/?id=798
Support should be added for MySQL to work over IPv6
("Internet Protocol Version 6").
This means:
- users can connect with IPv6. this is partly a connector problem.
- storage of user address information, e.g. in mysql.user, can be in IPv6 format
- the proposed new data types CIDR and INET allow IPv6 format as described in WL#2037 "Add CIDR and INET data types"
- functions like inet_ntoa() need revision
Try using this for your PDO conenction and see if it works.
$dbh = new PDO('mysql:host=<ipv6-address>;port=<port>;dbname=<dbname>', <dbusername>, <dbpassword>);
In case if fails, you can better use try...catch to get exactly at the error
<?php
try {
$dbh = new PDO('mysql:host=<ipv6-address>;port=<port>;dbname=<dbname>', <dbusername>, <dbpassword>);
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
?>

How to connect to remote MYSQL database via php

I have to build an android app which connects to a remote MYSQL webserver and retrieve information for display purpose.
So I did research coz I had no idea where to start. I read an article it mentions the better approach for that is to use php script. And didn’t know anything about how server, database and php works, so I studied php from here “http://www.homeandlearn.co.uk/php/php12p1.html” to understand and downloaded WhampServer to do some testing. On local machine everything worked fine.
But main thing I don’t understand is “HOW TO CONNECT TO REMOTE SERVER/DATABASE”.
It’s obvious that I’m doing something really stupid, I just need help to find out what am I doing wrong.
When we test php script on local machine in webrowse we use "localhost/some.php." But when I want to test same php script on remoter server from my local machine then what and how should I do? Do I need to make some changes in configuration file on server side?
I have done more research before asking this question here to understand remote server connection in php but I still don’t understand. I have gone through almost all the pages within the link below:
https://www.google.co.uk/search?q=connect+to+remote+mysql+webserver+php&rlz=1C1AVSX_enGB447GB448&oq=connect+to+remote+mysql+webserver+php&aqs=chrome.0.69i57j69i62l3.19724j0&sourceid=chrome&ie=UTF-8#fp=5a2359cf96dc5f79&q=how+to+connect+to+remote+mysql+web+server+php
And help would be much appreciated.
If my question is not clear please let me know I'll try to explain more. or think of it as if you have to connect to a remote MySQL server how would you do , means what is the process and steps involved in that.
Thanks everyone.
Edit
I have created a database "dealt3_raj_test" on remote server. and when I type "examplewebserver.CO.UK/myphpscriptname" in my web browser.
It gives me error "An error occurred , You have reached the error page"
<?PHP
$user_name = "dealt3_raj";
$password = "5dN5nh&eMd(vCR$dzk";
$database = "dealt3_raj_test";
$server = "examplewebserver.CO.UK";
$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);
if($db_handle)
{
print "Connected";
}
else
{
print "Can not connect to server";
}
if ($db_found)
{
print "DataBase found";
}
else
{
print "DataBase not found";
}
?>
Adding onto #user4035's comment, after opening the connection, use JDBC in your Android/Java code to interact with the database.
That said, it is not good practice. Rather create a web service
Your application may experience latency/connectivity issues. This will impact performance.
Your MySQL server will have to be open to remote connections which is strongly advised against
If your Android App is intended for public usage, it means the database username and password of your MySQL server reside on everyone's phone using your app. Encrypted or not this makes your database server a "step less" secure
Well answered here on SO (JDBC vs Web Service for Android)
use this code to connect with data base
$username = "database user name";
$password = "DB password";
$hostname = "hostname";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");
//select a database to work with
$selected = mysql_select_db("data base name",$dbhandle) or die("Could not select examples");

How to use a offsite mysql database

i hope this makes since so ill try my best to explain as much as i can
i have a personal server that i use for testing my scripts before i put them on my home page in my home page i have a script that uses a feed from a database on another server
how would i be able to get the contents from the off server to the server i am testing on can i do it like normal just change the urls?
e.g:
//Before//
$dbms = 'mysql';
$dbserver = 'localhost';
$dbport = '';
//after//
$dbms = 'mysql';
$dbserver = 'homesite.com';
$dbport = '21';
hope this makes since and if it has been answered before sorry just point me to right direction :)
Thanks
Rob
MySQL's usual port is 3306, so that not only needs to be forwarded through any router(s) there may be, but also allowed on any firewall(s). Further, the users themselves in MySQL need access, unless their host in the mysql.users table is set to %, which I don't recommend, however, instead have your IP and/or hostname (depending on MySQL's look up settings in my.ini), however if the IP address changes, you'll need to update this.
Some more info:
http://dev.mysql.com/doc/refman/5.1/en/adding-users.html

ibase_connect: local computer host, can't figure out working path

I may be dumb. Not sure yet.
Trying to do something that should be simple:
$database = '10.10.10.81:?????';
$username = 'admin';
$password = 'pw';
$conn = ibase_connect($database, $username, $password);
if (!$conn)
{
echo "Error while connecting: ".ibase_errmsg();
exit();
}
echo 'workan';
We hit the error: connection rejected by remote interface. From another question here I read that this comes from various things: Could be a user/password problem, could be a host ($database) problem, something else that I forget.
I believe my host string is terribly wrong and can't figure out how I should be going about this. Perhaps my google-fu is not up to par.
Trying to get to
E:\fishbowl\database\data\base.fdb
on 10.10.10.81, a separate local computer
Tried a bunch of combinations but can't seem to make anything work.
Edit: Just a heads up, this is a firebird database. Not sure if that changes things
For windows, it should look like this:
localhost/10095:C:/Data/SKLADFD.FDB.
See how port (in this case 10095) is specified.
In your case:
10.10.10.81/3050:E:/fishbowl/database/data/base.fdb
Path to the file must be local file on server, not windows share on connecting machine.

MySQL issue when moving from Dreamhost to HostGator

First, let me start by saying the website was working perfectly well on Dreamhost (except for the last week with horrible lags and downtime - but it was not what made me move away from them, read below).
I was hosting my website on Dreamhost. I had to move due to a problem with the owner of the revenue hosting (he'd not let me change a dns config on the panel nor would do it for me).
So I signed with HostGator, and now I'm stuck with this MySQL issue. Here's what I've done so far:
First, I uploaded all the files via ftp to the HostGator server. Then, I exported the database from the old server and imported it onto the new one. Then I changed the name server on the domain registrar, and finally, after a while, hit F5 on the browser.
And then there was a huge ammount of error lines. So I searched the HostGator Support Portal and found out that I had to change from this (the code I was using before):
<?
date_default_timezone_set('America/Sao_paulo');
if ($_SERVER['HTTP_HOST']=='localhost') {
$conexao = mysql_connect("localhost");
$bd = mysql_select_db("tablename");
} else {
$conexao = mysql_connect("mysite.phpmyadmin.com","mylogin","mypassword");
$bd = mysql_select_db("mydatabasename");
}
mysql_query("SET time_zone='-3:00'");
?>
To something like this:
<?
define('DB_NAME', 'mydatabasename');
define('DB_USER', 'mylogin');
define('DB_PASSWORD', 'mypassword');
define('DB_HOST', 'localhost');
?>
The thing is I'm no PHP or MySQL expert. The programmer let me without support, so I'm on my own.
When I try to load my site, it gives the following errors:
Warning: mysql_query() [function.mysql-query]: Access denied for user 'mylogin'#'localhost' (using password: NO) in /home/mylogin/public_html/index.php on line 42
Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /home/mylogin/public_html/index.php on line 42
ERROR 40 >Access denied for user 'mylogin'#'localhost' (using password: NO)
On the file index.php, line 42 is this:
$sqlo = mysql_query("select *, max-buyers as left from offers inner join rel_city on rel_city.id_offer = offers.id where id_city=$city and startdate <= now() and enddate >= now() order by priority desc, left desc, enddate asc") or die("ERROR 40 >".mysql_error());
One important detail is that I need these two lines to work with the new hosting, but whenever I try to insert them, it gives new errors:
date_default_timezone_set('America/Sao_paulo');
mysql_query("SET time_zone='-3:00'");
Any ideas of how I can get this working?
Try this:
<?
date_default_timezone_set('America/Sao_paulo');
$conexao = mysql_connect("localhost","mylogin","mypassword");
$bd = mysql_select_db("mydatabasename");
mysql_query("SET time_zone='-3:00'");
?>
What I've done is simplify the original code to remove the IF / ELSE statement which looks like it might be there for development purposes.
If you're getting other errors not posted, please include them.
I'm not familiar with HostGator's setup, but what seems to be the issue is either the address or credentials you are trying to access the server with.
Perhaps HostGator prepends a prefix to usernames and database names? (ex. maybe it should be something_mydatabasename rather than simply mydatabasename)
Or perhaps their setup doesn't support using localhost as the database address; perhaps they have a defined and isolated address for you to connect to.
I'd give support a shout, or double-check the addresses, usernames, and database names in your cPanel settings. It may give you a good start.
(This advice is, of course, assuming that you have the username and password for the database correct, which the error suggests is not the case.)
From the errors, it looks like you don't have the DB user and password setup in MySQL.
Your database has a username and password. Make sure that you have specified them correctly, or you won't be able to connect to your database.
The error messages you're seeing are due to your programmer using the PHP die() function, which isn't really good practice.
Check with your host and make sure that you have permissions on your tables, as well. You should, but you never know.

Categories