|
发表于 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]
如果上面这些源码你也看不懂,我就没办法了。 |
|