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实现的农历算法实例

本文实例讲述了php实现的农历算法。分享给大家供大家参考。具体如下: <?php function lunarcalendar ($month, $year) { g...

php curl 上传文件代码实例

假设server端上传文件处理脚本upload.php: 复制代码 代码如下: <?php    print_r($_POST);  p...

攻克CakePHP系列三 表单数据增删改

攻克CakePHP系列三 表单数据增删改

这里声明一点,上例中不小心把数据库表中lastupd字段错打成lastudp,本例子予以更正。 除上诉字段数据库与上例一致。 工程仍沿用上例,如下图: 代码依次为: database....

PHP自动选择 连接本地还是远程数据库

Mysql.class.php 文件见 https://www.jb51.net/article/25496.htm复制代码 代码如下: <?php // 包含Mysql操作类 i...

深入研究PHP中的preg_replace和代码执行

深入研究PHP中的preg_replace和代码执行

前言 本文将深入研究 preg_replace /e 模式下的代码执行问题,其中包括 preg_replace 函数的执行过程分析、正则表达式分析、漏洞触发分析,当中的坑非常多,相信看完...