如何解决如何使用PHP连接到PostgreSQL数据库?
您已经在标题中看到,我想连接到使用PHP和Yii2在服务器上创建的PostgreSQL数据库。不幸的是,对于这种工作,我是一个新手,我以前从未做过,所以我有几个问题希望我能回答。
- 我使用的是Yii2 basic,在config目录中有一个
#use this value for 'e',since my program is too big to paste here.I dont want to take much of your time e=[[1,"food",456,"21/1"],[2,"health",654,"16/10"],[3,"transport",6500,"13/12"]] monthlyincome=13000 def charts(): def spentandsaved(): plt.axis("equal") splist=[s,saved] lbl=['money spent','money saved'] plt.pie(splist,labels=lbl) plt.show() plt.close('all') def categories(): categories=[] amt=[] for i in range(len(e)): categories.append(e[i][1]) amt.append(e[i][2]) plt.barh(categories,amt) plt.ylabel('categories') plt.xlabel('amount') plt.show() plt.close('all') c='y' while True: print("1.View money spent vs money saved report(Press 1)") print("2.View Total Expense Report (Press 2)") print("3.View both reports") ch=int(input()) if ch==1: spentandsaved() elif ch==2: categories() elif ch==3: spentandsaved() categories() else: print("invalid choice!") c=input("Do you want to view the Report again(Y/N)") if c.lower()=='n': break charts()
文件,其中包含以下代码:
db.php
代码真的可以吗?当然,我将使用主机IP代替localhost。数据库 return [
'class' => 'yii\db\Connection','dsn' => 'pgsql:host=localhost;dbname=my1DB','username' => 'root','password' => '','charset' => 'utf8',];
实际上已创建,并且已经包含名为my1DB
的关系。
- 我在客户端上安装了Yii2 basic。现在我的问题是,是否还需要在服务器上安装它?这可能是一个愚蠢的问题,如果真的是,我真的很抱歉,但是我不确定我是否正确理解了这一切。因此,
countries
文件是否需要在服务器上?还是只安装在我的客户端上就可以了吗?
解决方法
db.php必须在运行PHP的服务器端,如果PostgreSQL服务器与PHP在同一台服务器上运行,则可以将其保留为localhost,并且应该可以正常工作,只需将确保凭据和数据库名称存在。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。