doujun5009 2013-03-25 12:17
浏览 60
已采纳

如何使用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

  • 写回答

1条回答 默认 最新

  • dpzr52746 2013-04-01 08:18
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作