search
Homephp教程php手册php如何连接和操作PostgreSQL数据库

php如何连接和操作PostgreSQL数据库

Jun 13, 2016 am 11:57 AM
phppostgresqldatabaseconnect

这篇文章主要介绍了php连接与操作PostgreSQL数据库的方法,以实例形式较为详细的分析了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&#39;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&#39;t fetch row from table."); 
//$serial_no= $row[&#39;serial_no&#39;]; 
//$name= $row[&#39;name&#39;]; 
//$birthday= $row[&#39;birthday&#39;]; 
//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&#39;t insert data to table.");
//if($reusult) 
//{ 
//echo "rechords inserted sucessfully!"; 
//} 
// 
pg_close($pg);

【相关教程推荐】

1. php编程从入门到精通全套视频教程
2. php从入门到精通 
3. bootstrap教程

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.