[Reprint]php Connection PostgreSQL database and its operation (PHP5,POSTGRESQL9)

Source: Internet
Author: User

Database connection:
dbconn.php
<?php
$conn = Pg_connect ("host=localhost port=5432 dbname=myd user=postgres password=postgres");

if ($conn) {
Print "Has connected". "<br>";
}else{
Print "Connect Failure". "<br>";
}
?>
insert operation:
insert.php
<?php
Require_once ' dbconn.php ';

Here is how PHP inserts the PostgreSQL database using prepared;
/*
* Note the points:
* 1, to set the parameters can only be a one-off from the next, but not anything else such as: $a is not legal.
*
* 2, Pg_prepare has three necessary parameters, the first one is the database link; the second is the name of our precompiled statement,
* To be used later, you can specify ""; the third is the SQL statement we want to execute.
*
* 3, execute the statement this list in the For loop, the first parameter is a link, the second parameter is to execute the precompiled statement, the third parameter is an array for
* Specify the parameters we have not set with the precompiled statement.
*/

$str = ' INSERT INTO test values ($1,$2) ';

Pg_prepare ($conn, "Mypre", $str);

for ($i = 0; $i < 3; $i + +):
Pg_execute ($conn, "Mypre", Array ($i. ' A ', $i. ' B '));
ENDfor;
?>

Query and its delete operations:
select.php
<?php
Require_once ' dbconn.php ';

$STR = "SELECT * from Test";

$resultSet = Pg_query ($conn, $STR);

while ($row = Pg_fetch_row ($resultSet)) {
Print $row [0]. ' '. $row [1]. " <br> ";
}
?>
The next is affter delete;
<br/>
<?php
/**
* The following is a method of deletion, is done with pg_delete, do not know whether can delete multiple lines of records, anyway, I have not realized yet.
*
*/
$arr = array (' id ' = ' 0A ');

Pg_delete ($conn, ' test ', $arr);

/**
* The following is implemented in the form of precompilation, but when there is no unknown parameter, a null array is passed to the Pg_execute function or an error is made.
*
*/
$remove = ' Delete from Test where id = $ ';

Pg_prepare ($conn, "Remove", $remove);


Pg_execute ($conn, "Remove", array (' id ' = ' 1 A '));

?>

<br/>
<?php
$resultSet = Pg_query ($conn, $STR);

while ($row = Pg_fetch_row ($resultSet)) {
Print $row [0]. ' '. $row [1]. " <br> ";
}
?>

Update operation:
update.php
<?php
Require_once ' dbconn.php ';

$update = ' Update test Set email = ' WHERE id = $ ';

Pg_prepare ($conn, ' Update ', $update);

$id = ' 1 a ';

Pg_execute ($conn, ' Update ', array (' email ' = ' 111 ', ' id ' = $id));

?>

Note: The table structure is as follows in the code because of the related statements in the table fields:

CREATE TABLE Test (ID Cahr (8), email char (8));

[Reprint]php Connection PostgreSQL database and its operation (PHP5,POSTGRESQL9)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.