如何使用PHP从我的计算机连接到远程数据库

如何使用PHP从我的计算机连接到远程数据库

问题描述:

I am quite new in PHP and databases. I am trying to connect to a remote Postgres database from my computer. In the past I have done this by using a localhost and a MySQL database but it is the first time that I try to connect in another host. This is the PHP code that I have:

  <?php
  $conn_string = "host=hostname.com port=xxxx dbname=dbname user=root password=root";

  $dbconn = pg_connect($conn_string);
  ?>

I read several articles in internet saying that it is enough to change the hostname from localhost to the one provided but it doesn't seem to work. I also have deactivated my firewall in case there is some problem with the communication but with no success.

As I said I am quite new with this and in the past I had used only test servers (localhost). Back then I was using software like Wamp and Mamp and I was placing the script files in the localhost.

So, my questions are: 1) where should I save the test.php file which has the above lines of code? 2) Would this solve the problem or I am missing some important concept here?

I hope what I am trying to say is clear. Please let me know if I have to be more specific in something.

Thanks Dimtiris

First, error messages would really help. Based on the comments I will say I suspect that you are connecting to the right server. However you are using a username of "root?" It would be highly unusual for such a username to be present on Postgres.

The first one is where to put the file. This should be put in your web server's document root or cgi bin depending on how you have things configured (probably the former).

For your second question, please double check usernames and passwords, and make sure you can use those usernames and passwords via pgAdmin III to connect to the same db. Making sure you have the credentials right to start will make your code a lot easier to troubleshoot if things don't go right.