[英]How to connect PostgreSQL and PHP on mac?
I am new to PHP. 我是PHP新手。 I want to use the database stored in pgSQL from my PHP file.
我想使用我的PHP文件中存储在pgSQL中的数据库。 Please tell how to integrate pgSQL and PHP, so that I can use pgSQL in PHP using
pg_connect()
. 请告诉我如何集成pgSQL和PHP,以便可以使用
pg_connect()
在PHP中使用pgSQL。 My php.info()
is showing MySQL, pgSQL, SQLite enabled under PDO support. 我的
php.info()
显示了在PDO支持下启用的MySQL,pgSQL,SQLite。 But when I use pg_connect()
it says: 但是当我使用
pg_connect()
它说:
Warning: pg_connect(): Unable to connect to PostgreSQL server: FATAL: role "postgres" does not exist in /Users/username/Sites/index.php on line 3
Warning: pg_last_error(): No PostgreSQL link opened yet in /Users/username/Sites/index.php on line 4
Warning: pg_last_error(): supplied resource is not a valid PostgreSQL link resource in /Users/username/Sites/index.php on line 4
Could not connect:
I have used this tutorial to run PHP on Mac. 我已使用本教程在Mac上运行PHP。 But I couldn't find a tutorial clearly showing how to integrate PHP and pgSQL.
但是我找不到一个清楚地说明如何集成PHP和pgSQL的教程。
https://www.dyclassroom.com/howto-mac/how-to-install-apache-mysql-php-on-macos-mojave-10-14 https://www.dyclassroom.com/howto-mac/how-to-install-apache-mysql-php-on-macos-mojave-10-14
<?php
// Connecting, selecting database
$dbconn = pg_connect("host=localhost dbname=ipl user=postgres password=password")
or die('Could not connect: ' . pg_last_error());
// Performing SQL query
$query = 'SELECT * FROM player';
$result = pg_query($query) or die('Query failed: ' . pg_last_error());
// Printing results in HTML
echo "<table>\n";
while ($line = pg_fetch_array($result, null, PGSQL_ASSOC)) {
echo "\t<tr>\n";
foreach ($line as $col_value) {
echo "\t\t<td>$col_value</td>\n";
}
echo "\t</tr>\n";
}
echo "</table>\n";
// Free resultset
pg_free_result($result);
// Closing connection
pg_close($dbconn);
?>
I got the problem. 我有问题。 My username is not postgres but my name.
我的用户名不是postgres,而是我的名字。 I must have set it while installing Postgres.
我必须在安装Postgres时进行设置。 Therefore it is saying no role as 'postgres' exist.
因此,它说不存在作为“ postgres”的角色。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.