Home  >  Article  >  Backend Development  >  How to connect and operate PostgreSQL database in PHP, postgresql database_PHP tutorial

How to connect and operate PostgreSQL database in PHP, postgresql database_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:10:46863browse

How to connect and operate PostgreSQL database with PHP, postgresql database

The example in this article describes how to connect and operate the PostgreSQL database in PHP. Share it with everyone for your reference.

The specific implementation method is as follows:

Copy code The code is as follows:

$pg=@pg_connect("host=localhost user=postgres password=sa dbname=employes")
or die("can't connect to database.");
$query="select * from employees order by serial_no";
//$query="insert into employees 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); //How many records are output to be queried
//if($result)
//{
//echo "recrods inserted successfully!";
//echo pg_affected_rows($result);//Output how many records have been inserted
//}
//Example 1 [pg_fetch_row]
echo "";
echo "";
echo "";
echo"";
echo"";
echo"";
for($i=0;$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"
";
echo"";
echo"";
echo"";
echo"";
}
echo"
serial_nonamebirthday
$serial_no$name$birthday
";
//Example 2 [pg_fetch_array]
//echo "";
//echo "";
//echo "";
//echo"";
//echo"";
//echo"";
//
//for($i=0;$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"
";
//echo"";
//echo"";
//echo"";
//echo"";
//
//}
//echo"
serial_nonamebirthday
$serial_no$name$birthday
";
//Add, delete, modify instances
//$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 successfully!";
//}
//
pg_close($pg);

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/932488.htmlTechArticleHow to connect and operate PostgreSQL database with php, postgresql database This article describes the method of connecting and operating PostgreSQL database with php. Share it with everyone for your reference. Specific actual...
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