当前位置:首页 > PHP教程 > php高级应用 > 列表

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

发布:smiling 来源: PHP粉丝网  添加日期:2014-09-11 22:39:28 浏览: 评论:0 
  1. $pg=@pg_connect("host=localhost user=postgres password=sa dbname=employes") 
  2. or die("can't connect to database."); 
  3. $query="select * from employes order by serial_no"; 
  4. //$query="insert into employes values(10008,'susan','1985-09-04','80','50')"; 
  5. $result=@pg_query($pg,$query) or die("can't run query to table."); 
  6. //echo pg_num_rows($result); //输出多少条记录被查询 
  7. //if($result) 
  8. //{ 
  9. //echo "recrods inserted sucessfully!"; 
  10. //echo pg_affected_rows($result);//输出多少条记录被插入 
  11. //} 
  12. //实例一[pg_fetch_row] 
  13. echo "<table border=1>"; 
  14. echo "<tr>"; 
  15. echo "<td>serial_no</td>"; 
  16. echo"<td>name</td>"; 
  17. echo"<td>birthday</td>"; 
  18. echo"</tr>"; 
  19. for($i=0;$i<pg_num_rows($result);$i++) 
  20. { 
  21. $row=@pg_fetch_row($result) or die("can't fetch row from table."); 
  22. $serial_no= $row[0]; 
  23. $name= $row[1]; 
  24. $birthday= $row[2]; 
  25. echo"<tr>"; 
  26. echo"<td>$serial_no</td>"; 
  27. echo"<td>$name</td>"; 
  28. echo"<td>$birthday</td>"; 
  29. echo"</tr>"; 
  30. } 
  31. echo"</table>"; 
  32. //实例二[pg_fetch_array] 
  33. //echo "<table border=1>"; 
  34. //echo "<tr>"; 
  35. //echo "<td>serial_no</td>"; 
  36. //echo"<td>name</td>"; 
  37. //echo"<td>birthday</td>"; 
  38. //echo"</tr>"; 
  39. // 
  40. //for($i=0;$i<pg_num_rows($result);$i++) 
  41. //{ 
  42. // 
  43. //$row=@pg_fetch_array($result) or die("can't fetch row from table."); 
  44. //$serial_no= $row['serial_no']; 
  45. //$name= $row['name']; 
  46. //$birthday= $row['birthday']; 
  47. //echo"<tr>"; 
  48. //echo"<td>$serial_no</td>"; 
  49. //echo"<td>$name</td>"; 
  50. //echo"<td>$birthday</td>"; 
  51. //echo"</tr>"; 
  52. // 
  53. //} 
  54. //echo"</table>"; 
  55. //增加,删除,修改实例 
  56. //$newrow=array("serial_no"=>"1006","name"=>"peter","birthday"=>"1990-07-03","salary"=>"90","bonus"=>"80"); 
  57. //$reusult=@pg_insert($pg,"employes",$newrow) or die("can't insert data to table.");//开源代码phpfensi.com 
  58. //if($reusult) 
  59. //{ 
  60. //echo "rechords inserted sucessfully!"; 
  61. //} 
  62. // 
  63. pg_close($pg);

Tags: PostgreSQL连接数据 PostgreSQL实例

分享到: