LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1662|回复: 2

php连接postgresql

[复制链接]
发表于 2005-9-18 19:33:15 | 显示全部楼层 |阅读模式
要对PHP做怎样的设置,才能连接到postgresql数据库呢?
我看过php 文档,还是不明白,希望高人能指点一下。
发表于 2005-9-18 22:22:19 | 显示全部楼层
看来你不喜欢搜索论坛,就在这个论坛中我就有示例。再发一遍》》》
1、用adodb联结:
[php]
<?php
  Session_Start();
  $user=$_POST["user"];
   $pwd=$_POST["pwd"];
   if(isset($user)) {
   $_SESSION["user"]=$user;
   $_SESSION["pwd"]=$pwd;};
   session_write_close ();
   error_reporting(0);
   include_once('/usr/share/php/adodb/adodb-errorhandler.inc.php');
   include_once('/usr/share/php/adodb/adodb.inc.php');
   $db =&ADONewConnection('postgres'); # eg 'mysql' or 'postgres'
   // $db->debug = true;
    $host='xxx.xxxx.xxx';
    $port='5432';
    $dbname='test';
    $user=$_SESSION["user"];
    $password=$_SESSION["pwd"];
    $conn_string = "host=$host dbname=$dbname user=$user password=$password port=$port";
     $db->Connect($conn_string) or die("can't connect!");
?>       
[/php]
2、直接联结:
[php]
<?php
//dl('pgsql.so');
Session_Start();
  $user=$_POST["user"];
   $pwd=$_POST["pwd"];
   if(isset($user)) {
   $_SESSION["user"]=$user;
   $_SESSION["pwd"]=$pwd;};
   session_write_close ();
   echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">';

$host='xxxx.xxxx.xxxx';
$port='5432';
$dbname='test';
$conn_string = "host=$host dbname=$dbname user=$user password=$password port=$port";
$dbconn = pg_connect($conn_string) or die("Could not connect");
if  ($dbconn){echo 'ok';}else{echo 'fales';};
print_r(pg_version());

$query='select * from bzzl';
$rs=pg_query($dbconn,$query);
$fd=pg_fetch_assoc($rs);

echo "\n";
$myfd=$fd['dw'];
echo $myfd;

pg_close($dbconn);
?>
[/php]

3、使用pdo联结:

[php]
<?php
// Connect to an ODBC database using driver invocation
$htmm='<html>';
$htmm.='<head>';
$htmm.="<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>";
$htmm.='<title>产品基础数据</title>';
$htmm.='</head>';
echo $htmm;
$host='xxx.xxxx.xxxx';
$port='5433';
$dbname='test';
$user='xxxxxx';
$password='xxxxxxxxxxxxx';
//$cc = "host=$host,dbname=$dbname,port=$port";
$dsn = "pgsql:host=$host port=$port dbname=$dbname";


try {
   $dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
   echo 'Connection failed: ' . $e->getMessage();
}
//echo "<p>";
echo '<table border=1 borderwidth=0>';
$sql='select * from cpjcsj';
print '<tr><th>产品代码</th><th>产品代号</th><th>产品名称</th></tr>';
   foreach ($dbh->query($sql) as $row) {
      print '<tr><td>'.$row['cpdm'].'</td>';
      print '<td>'.$row['cpdh'] . '</td>';
      print '<td>'.$row['cpmc'] . '</td></tr>';
   }
// echo "</p>";
echo "</table>";
$dbh->disconnect;
?>
[/php]

使用pear-db联结
[php]
<?php
require_once 'DB.php';

$dsn = array(
    'phptype'  => 'pgsql',
    'username' => 'xxxxxxxx',
    'password' => 'xxxxxxxxxx',
    'hostspec' => 'xxxx.xxxx.xxxx',
    'database' => 'xxxxx',
            'port'    =>  '5433'
);

$options = array(
    'debug'       => 2,
    'portability' => DB_PORTABILITY_ALL,
);

$db =& new DB;
$conn=$db->connect($dsn, $options);
if (PEAR::isError($db)) {
    die($db->getMessage());
}else{echo 'dbconnect ok';}
$sql='select * from cpjcsj;';
$sth=&$conn->prepare($sql);
$res=&$conn->execute($sth);
if (PEAR::isError($res)) {
    die($res->getMessage());}
while ($res->fetchInto($row)) {
    // Assuming DB's default fetchmode is DB_FETCHMODE_ORDERED
    echo $row[1] . "\n";
}
[/php]
如果上面这些源码你也看不懂,我就没办法了。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-9-19 09:13:49 | 显示全部楼层
谢谢拉。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表