A-A+

PostgreSQL连接数据的方法和操作实例

2012年05月23日 PHP技术文章 暂无评论 阅读 89 views 次

PostgreSQL是一个自由的对象-关系数据库服务器(数据库管理系统),其实已经有许多php应用程序在使用这种数据库了,尤其最近几年PostgreSQL数据库的发展劲头挺大,作为php程序员,必须要熟悉 PostgreSQL连接数据的方法和操作,下面是一个PostgreSQL连接数据的方法和操作实例,方便在使用时查询。

<?php   
$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);//输出多少条记录被插入   
//}   
  
//实例一&#91;pg_fetch_row&#93;   
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&#91;0&#93;;   
$name= $row&#91;1&#93;;   
$birthday= $row&#91;2&#93;;   
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&#91;'serial_no'&#93;;   
//$name= $row&#91;'name'&#93;;   
//$birthday= $row&#91;'birthday'&#93;;   
//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);   
?>
标签:

给我留言