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

yipeiwu_com5年前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 if (PHP_SAPI != 'cli') { exit('Pl...

php字符串函数学习之substr()

<?php /* 定义和用法 substr() 函数返回提取的子字符串, 或者在失败时返回 FALSE。 语法 substr(string,start,length) 参数...

php实现获取文件mime类型的方法

本文实例讲述了php获取文件mime类型的方法。分享给大家供大家参考。具体如下: 1.使用 mime_content_type 方法 string mime_content_type...

php用户名的密码加密更安全的方法

php用户名的密码加密更安全的方法

php中对用户密码的加密主要有两种方法,一种是利用md5加密,另一种是利用password_hash加密,两种方法中后一种的方法比前一种方法安全很多,几乎不能被黑客破解,但php版本必须...

mayfish 数据入库验证代码

一般在把数据写入数据库之前,先对将要写入的数据进行校验,可以避免出现比较严重的安全问题(例如一般性的SQL注入攻击)。 mayfish 可以灵活的自定义将要执行写入的数据内容的校验规则,...