php连接与操作PostgreSQL数据库的方法

yipeiwu_com6年前PHP代码库

本文实例讲述了php连接与操作PostgreSQL数据库的方法。分享给大家供大家参考。

具体实现方法如下:

复制代码 代码如下:

$pg=@pg_connect("host=localhost user=postgres password=sa dbname=employes")
or die("can't connect to database.");
$query="select * from employes order by serial_no";
//$query="insert into employes values(10008,'susan','1985-09-04','80','50')";
$result=@pg_query($pg,$query) or die("can't run query to table.");
//echo pg_num_rows($result); //输出多少条记录被查询
//if($result)
//{
//echo "recrods inserted sucessfully!";
//echo pg_affected_rows($result);//输出多少条记录被插入
//}
//实例一[pg_fetch_row]
echo "<table border=1>";
echo "<tr>";
echo "<td>serial_no</td>";
echo"<td>name</td>";
echo"<td>birthday</td>";
echo"</tr>";
for($i=0;$i<pg_num_rows($result);$i++)
{
$row=@pg_fetch_row($result) or die("can't fetch row from table.");
$serial_no= $row[0];
$name= $row[1];
$birthday= $row[2];
echo"<tr>";
echo"<td>$serial_no</td>";
echo"<td>$name</td>";
echo"<td>$birthday</td>";
echo"</tr>";
}
echo"</table>";
//实例二[pg_fetch_array]
//echo "<table border=1>";
//echo "<tr>";
//echo "<td>serial_no</td>";
//echo"<td>name</td>";
//echo"<td>birthday</td>";
//echo"</tr>";
//
//for($i=0;$i<pg_num_rows($result);$i++)
//{
//
//$row=@pg_fetch_array($result) or die("can't fetch row from table.");
//$serial_no= $row['serial_no'];
//$name= $row['name'];
//$birthday= $row['birthday'];
//echo"<tr>";
//echo"<td>$serial_no</td>";
//echo"<td>$name</td>";
//echo"<td>$birthday</td>";
//echo"</tr>";
//
//}
//echo"</table>";
//增加,删除,修改实例
//$newrow=array("serial_no"=>"1006","name"=>"peter","birthday"=>"1990-07-03","salary"=>"90","bonus"=>"80");
//$reusult=@pg_insert($pg,"employes",$newrow) or die("can't insert data to table.");
//if($reusult)
//{
//echo "rechords inserted sucessfully!";
//}
//
pg_close($pg);

希望本文所述对大家的PHP程序设计有所帮助。

相关文章

PHP经典Redis操作类 调用方便,操作简单

/**  * redis操作类  * 说明,任何为false的串,存在redis中都是空串。  * 只有在key不存在时,才会返回fa...

PHP解决URL中文GBK乱码问题的两种方法

一般情况下对URL进行UrlEncode 处理 , urlencode(),urldecode(); 也可以通过iconv('utf-8', 'gb2312', $ret)对接收到的数据...

php中数字0和空值的区别分析

作为一个合格的php程序员,一些基础知识是必须要知道的,例如0和空的区别,关于这个区别,下面就通过几个实例进行简单的分析,其中的道理,只可意会,不可言传,读者可以自己去慢慢体会了。 复制...

php 友好URL的实现(吐血推荐)

友好URL的实现(吐血推荐) 大家经常看到别的站的URL是这样的吧? http://www.xxx.com/module/show/action/list/page/7 或者 http:...

PHP自定义函数实现数组比较功能示例

本文实例讲述了PHP自定义函数实现数组比较功能。分享给大家供大家参考,具体如下: <?php //数组使用标准比较运算符这样比较的 function standar...