This article mainly introduces how to connect to and operate a PostgreSQL database in php, and analyzes in detail how to connect php to a PostgreSQL database in the form of an instance, as well as reading, adding, modifying, and deleting skills, has certain
This article mainly introduces how to connect to and operate a PostgreSQL database in php, and analyzes in detail how to connect php to a PostgreSQL database in the form of an instance, as well as reading, adding, modifying, and deleting skills, has certain
This article describes how to connect to and operate a PostgreSQL database using php. Share it with you for your reference.
The specific implementation method is as follows:
The Code is as follows:
$ Pg = @ pg_connect ("host = localhost user = s 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 ', '2017-09-04', '80', '50 ')";
$ Result = @ pg_query ($ pg, $ query) or die ("can't run query to table .");
// Echo pg_num_rows ($ result); // number of records output to be queried
// If ($ result)
//{
// Echo "recrods inserted sucessfully! ";
// Echo pg_affected_rows ($ result); // number of output records inserted
//}
// Instance 1 [pg_fetch_row]
Echo"
";Echo"
";Echo"
Serial_no | ";Echo"
Name | ";Echo"
Birthday | ";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"
$ Serial_no | ";Echo"
$ Name | ";Echo"
$ Birthday | ";Echo"
";}Echo"
";
// Instance 2 [pg_fetch_array]
// Echo"
";// Echo"
";// Echo"
Serial_no | ";// Echo"
Name | ";// Echo"
Birthday | ";// 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"
$ Serial_no | ";// Echo"
$ Name | ";// Echo"
$ Birthday | ";// Echo"
";////}// Echo"
";
// Add, delete, and modify an instance
// $ Newrow = array ("serial_no" => "1006", "name" => "peter", "birthday" => ", "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 );
I hope this article will help you with PHP programming.
,