php连接postgresql

时间:2023-08-06 21:27:14

在ubuntu下用php连接postgresql需要下个模块php5-pgsql

连接数据库并显示一张表的内容:

<?php
#连接数据库
$conn = pg_connect("host=localhost port=5432 dbname=test1 user=jm password=123");
if (!$conn)
echo "连接失败!!!!!<br>";
else
echo "连接成功=====@@@@======<br>"; #执行查询语句
$sql="select * from book";
$ret = pg_query($conn, $sql); #显示数据表
while($row = pg_fetch_row($ret)){
echo "ID = ". $row[0] . "\n";
echo "NAME = ". $row[1] ."\n";
echo "ADDRESS = ". $row[2] ."\n";
echo "SALARY = ".$row[4] ."\n\n";
echo "<br>";
} pg_close($conn);
?>