PHP 操作 PostgreSQL資料庫

來源:互聯網
上載者:User

1.

要讓PHP支援PostgreSQL,就需要重新編譯PHP;

./configure   --prefix=/usr/local/php5  --with-apxs2=/usr/local/apache2/bin/apxs --with-mysql=/usr/local/mysql --with-config-file-path=/usr/local/php5 --with-zlib --enable-mbstring=all --with-mysqli=/usr/local/mysql/bin/mysql_config --with-pdo-mysql=/usr/local/mysql --with-pgsql=/usr/local/pgsql

最後一個參數指明pgsql的路徑(注意,這是你自己的pgsql路徑!)


然後:

make

sudo make install



2.

如果已經啟動了Apache,需要重啟Apache:

sudo apachectl restart



3.

為了測試,我們先建一個測試資料庫:

在終端輸入以下命令:

createdb classdb

psql classdb

create table class(id int, name varchar(20), email varchar(20));



4.

在Apache的Web根目錄下建立一個index.php檔案,內容如下:

<?php
$conn = pg_connect("host=localhost port=5432 dbname=classdb user=postgresql password=postgresql");
if($conn){
    print "OK! Has connected" . "<br>";
}else{
    print "Error! Connect failure" . "<br>";
}
?>

注意,需要修改pg_connect的相關參數!(5432是pgsql的預設連接埠,就像mysql的3306連接埠)


顯示OK! Has connected. 表示已串連上pgsql。



5.

然後我們在php中插入記錄到pgsql中,修改index.php如下:

<?php
$conn = pg_connect("host=localhost port=5432 dbname=classdb user=postgresql password=postgresql");


if($conn)
{
  print "OK! Has connected" . "<br>";
}
else
{
  print "Error! Connect failure" . "<br>";
}
?>

</br>

<form action="index.php" method="post">
  <table>
    <caption><strong>Insert</strong></caption>
    <tr>
      <td><strong>id:</strong></td>
      <td><input type="text" name="id"></input></td>
    <tr>
      <td><strong>name:</strong></td>
      <td><input type="text" name="name"></input></td>
    <tr>
      <td><strong>email:</strong></td>
      <td><input type="text" name="email"></input></td>
    <tr>
    <tr>
      <td><input type="submit" value="INSERT"></input></td>
    </tr>
  </table>
</form>

<?php
  // insert
  $id    = $_POST["id"];
  $name  = $_POST["name"];
  $email = $_POST["email"];
  
  if($id && $name && $email)
  {
    $query = "INSERT INTO class VALUES($id, '$name', '$email')";
    $result = pg_query($query);
  }

  // select
  $query = 'SELECT * FROM class';
  $result = pg_query($query);
?>

<table border="1">
  <tr><th>id</th><th>name</th><th>email</th></tr>

<?php
while ($line = pg_fetch_array($result, null, PGSQL_ASSOC))
{
  echo "<tr>";
  foreach ($line as $col_value)
  {
    echo "<td>$col_value</td>";
  }
  echo "</tr>";
}

echo "</table>";

// 釋放結果集
pg_free_result($result);

// 關閉串連
pg_close($conn);

?>


在瀏覽器中:http://localhost/index.php 即可看到效果。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.