请问谁有现成的PHP跟postgresql数据库连接的源码吗???我是刚学的一名女生,希望谁能帮帮我,晚辈感激不尽!!! 我的意思想独立建立一个数据库连接,SQL语句操作的文件,然后当要用到数据库的时候,再调用它就行了, 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 <?php $database = pg_connect ("dbname=jacarta"); pg_query ($database, "begin"); $oid = pg_lo_create ($database); echo "$oid\n"; $handle = pg_lo_open ($database, $oid, "w"); echo "$handle\n"; pg_lo_write ($handle, "large object data"); pg_lo_close ($handle); pg_query ($database, "commit");?> <?//// 简单够用的MySQL封装库,改写自PHPLib7.2,继承PEAR::DB的风格// 连接参数由类构造时传入// 使用方法// 1 声明类实体 $db = new DB("mysql://root:@localhost:3306/test");// 2 输入SQL查询 $result = $db->query($SQL);// 3 取出结果集中的一行数据 $record = $result->fetchRow();// 4 直接取数据使用 $record['var_name']; (若执行的SQL语句无返回结果集,3-4 两步可跳过,比如 INSERT UPDATE DELETE )// 5 关闭数据库连接 $db->close();//class DB{ var $host; //连接的服务器名 var $port; //端口号 var $dbname; //所连接的数据库名 var $user; //拥护名 var $password; //用户的密码 var $sql // // 构造函数,采集需要的数据 // function DB($dsn = "mysql://root:@localhost:3306/test", $debug = false, $fetchMode = 0) { $this->dsn = $this->parseDSN($dsn); $this->cfg_debug = is_bool($debug) ? $debug : false; $this->cfg_fetch = $fetchMode ? $fetchMode : $this->cfg_fetch; } // // 打开数据库连接 // 根据$this->cfg_pConnect开关决定是否打开持续连接 // function connect() { if($this->link == NULL){ if($this->cfg_pConnect){ $this->link = @mysql_pconnect($this->dsn['host'], $this->dsn['username'], $this->dsn['password']); } else{ $this->link = @mysql_connect($this->dsn['host'], $this->dsn['username'], $this->dsn['password']); } if(!$this->link || !@mysql_select_db($this->dsn['database'], $this->link)){ $this->halt(); } } return $this->link; } // // 提交SQL查询,返回执行后的结果集 // 根据$this->cfg_debug开关打印查询语句以及执行时间 // function query($SQL) { $this->queryNo++; if(!empty($SQL) && $this->connect()){ $time_start = $this->getMicroTime(); $result = @mysql_query($SQL); $time_end = $this->getMicroTime(); if($this->cfg_debug){ //若有Debug参数,打印出SQL的相关信息。 $exeTime = round($time_end - $time_start, 6); $SQL = htmlspecialchars($SQL); print("Query:{$this->queryNo} Exec:{$exeTime}(S) SQL:{$SQL}<BR />"); } if(!$result) if($this->cfg_debug) $this->halt($SQL); else $this->halt("无"); if($result === TRUE) return true; $this->result = & new DB_result($result, $this->cfg_fetch); } return $this->result; } // // 关闭数据库连接 // pconnect打开的持续连接最好也执行close // function close() { if($this->link != NULL) @mysql_close($this->link); } function setFetchMode($fetchMode) { $this->cfg_fetch = $fetchMode ? $fetchMode : $this->cfg_fetch; } // // 返回autoincrement的生成的值 // function insertId() { return @mysql_insert_id($this->link); } // // 返回上次SQL查询影响的行数 // function affectedRows() { return @mysql_affected_rows($this->link); } // // 取得查询次数统计 // function getQueryNo() { return $this->queryNo; } // // 记录可能产生的错误 // function logErr() { $this->errNo = @mysql_errno($this->link); $this->errMsg = @mysql_error($this->link); } // // 数据库出错后的处理函数 // 根据$this->cfg_haltOnErr开关决定是否显示错误报告并挂起程序 // function halt($msg = "") { if($this->cfg_debug) ob_end_flush(); else ob_end_clean(); $time = date('Y-m-d H:i:s'); $error = @mysql_error(); $errno = @mysql_errno(); $page = $_SERVER['PHP_SELF']; $content = DB_ERROR; $content = str_replace("{copyrights}", $this->copyRights, $content); $content = str_replace("{version}", $this->version, $content); $content = str_replace("{error}", $error, $content); $content = str_replace("{errno}", $errno, $content); $content = str_replace("{time}", $time, $content); $content = str_replace("{page}", $page, $content); $content = str_replace("{msg}", $msg, $content); print($content); $this->logErr(); exit(); } function getMicroTime(){ list($usec, $sec) = explode(" ", microtime()); return ((float)$usec + (float)$sec); } function parseDSN($dsn) { $parsed = array( 'protocol' => "", 'username' => "", 'password' => "", 'host' => "", 'port' => "", 'database' => "", ); // Find protocol // $dsn => protocol://username:password@host:port/database if (($pos = strpos($dsn, '://')) !== false) { $parsed['protocol'] = substr($dsn, 0, $pos); $dsn = substr($dsn, $pos + 3); } else { return $parsed; } // Get (if found): username and password // $dsn => username:password@host:port/database if (($at = strrpos($dsn,'@')) !== false) { $str = substr($dsn, 0, $at); $dsn = substr($dsn, $at + 1); if (($pos = strpos($str, ':')) !== false) { $parsed['username'] = rawurldecode(substr($str, 0, $pos)); $parsed['password'] = rawurldecode(substr($str, $pos + 1)); } else { $parsed['username'] = rawurldecode($str); } } // Get (if found): host and port // $dsn => host:port/database if (($at = strrpos($dsn, '/')) !== false) { $str = substr($dsn, 0, $at); $dsn = substr($dsn, $at + 1); if (($pos = strpos($str, ':')) != false) { $parsed['host'] = rawurldecode(substr($str, 0, $pos)); $parsed['port'] = rawurldecode(substr($str, $pos + 1)); } else { $parsed['host'] = rawurldecode($str); } } // Get dabase if any // $dsn => database if ($dsn) { $parsed['database'] = rawurldecode($dsn); } return $parsed; }}class DB_result{ var $result = NULL; var $fetchMode = NULL; function DB_result($result, $fetchMode = DB_FETCHMODE_ASSOC) { $this->result = $result; $this->fetchMode = $fetchMode; } // // 取得结果集中的下一条数据 // 正常取出返回true // 数据已取完返回false // function fetchRow() { $record = NULL; if($this->result){ switch($this->fetchMode){ case DB_FETCHMODE_ROW : $record = @mysql_fetch_row($this->result); break; case DB_FETCHMODE_ASSOC : $record = @mysql_fetch_assoc($this->result); break; case DB_FETCHMODE_ARRAY : $record = @mysql_fetch_array($this->result); break; case DB_FETCHMODE_OBJECT : $record = @mysql_fetch_array($this->result); break; } if(!is_array($record)) $this->free(); } return $record; } // // 返回结果集中的数据行数 // function numRows() { if($this->result) return @mysql_num_rows($this->result); else return 0; } // // 释放结果集 // function free() { if($this->result != NULL){ @mysql_free_result($this->result); $this->result = NULL; } }}define('DB_FETCHMODE_ROW', 1);define('DB_FETCHMODE_ASSOC', 2);define('DB_FETCHMODE_ARRAY', 3);define('DB_FETCHMODE_OBJECT', 4);define('DB_ERROR', "<html><head><meta http-equiv='Content-Type' content='text/html; charset=gb2312'><title>Database Error</title></head><style> P, TEXTAREA, BODY { FONT-FAMILY:Verdana, Arial, Helvetica; FONT-SIZE:9pt; } </style><body><blockquote><p> </p><p><strong>数据库好象发生了一些微小的错误.</strong><br><br>请按浏览器的 <a href='javascript:window.location=window.location;'>刷新</a> 按钮重试</p><br><p>给您带来不便与困扰,我们着实深感抱歉...</p><textarea rows='15' cols='60'>数据库错误报告来自于 {copyrights} {version}错误信息:{error}错误号码:{errno}时间:{time}页面:{page}附注:{msg}</textarea></blockquote></body></html>");?>其实我想要的是像我上面那段代码的功能,有谁有吗?或者能否帮我修改成那样的代码啊??我现在急用,由于对这个不熟悉,所以我一点思路都没有,谢谢各位了先!!! 你把mysql换成pgsql的即可pg_connect("host=myHost port=myPort tty=myTTY options=myOptions dbname=myDB user=myUser password=myPassword"); 连接字符串可以这样写$dbconn2 = pg_connect("host=localhost port=5432 dbname=mary");但在PEAR::DB中的写法我记不得了,我要回家看了才知道 pgsql跟mysql差别不大呀为什么不使用pear::db或者adodb直接的类库?晕是不是你已经写了类库,以前用的是mysql,现在想改用pgsql? 就因为你写出了是“女生”,我才BSN,why?---恶狠狠的一个白眼!哼! 这样写$dsn = pgsql://username:password@localhost:5432/database 什么世道,女生就很了不起啊 -_-!PHPLIB中有PostgreSQL的类,参考一下吧 求助 php 实现分页? 数据 双重排序问题 mysql 如何一次delete多条信息 有开发过PAYPAL接口的进,传值问题! mysql问题 请诸位推荐本php好书谢谢~`` 这句怎么改改? WEB服务器要配置到高的 怎样可以修改服务器的时间,高分解答!!! 关于微信 setcookie后,cookie的内容能加密吗? 还是session的问题
$database = pg_connect ("dbname=jacarta");
pg_query ($database, "begin");
$oid = pg_lo_create ($database);
echo "$oid\n";
$handle = pg_lo_open ($database, $oid, "w");
echo "$handle\n";
pg_lo_write ($handle, "large object data");
pg_lo_close ($handle);
pg_query ($database, "commit");
?>
// 简单够用的MySQL封装库,改写自PHPLib7.2,继承PEAR::DB的风格
// 连接参数由类构造时传入
// 使用方法
// 1 声明类实体 $db = new DB("mysql://root:@localhost:3306/test");
// 2 输入SQL查询 $result = $db->query($SQL);
// 3 取出结果集中的一行数据 $record = $result->fetchRow();
// 4 直接取数据使用 $record['var_name']; (若执行的SQL语句无返回结果集,3-4 两步可跳过,比如 INSERT UPDATE DELETE )
// 5 关闭数据库连接 $db->close();
//class DB
{
var $host; //连接的服务器名
var $port; //端口号
var $dbname; //所连接的数据库名
var $user; //拥护名
var $password; //用户的密码
var $sql
//
// 构造函数,采集需要的数据
//
function DB($dsn = "mysql://root:@localhost:3306/test", $debug = false, $fetchMode = 0)
{
$this->dsn = $this->parseDSN($dsn);
$this->cfg_debug = is_bool($debug) ? $debug : false;
$this->cfg_fetch = $fetchMode ? $fetchMode : $this->cfg_fetch;
} //
// 打开数据库连接
// 根据$this->cfg_pConnect开关决定是否打开持续连接
//
function connect()
{
if($this->link == NULL){
if($this->cfg_pConnect){
$this->link = @mysql_pconnect($this->dsn['host'], $this->dsn['username'], $this->dsn['password']);
}
else{
$this->link = @mysql_connect($this->dsn['host'], $this->dsn['username'], $this->dsn['password']);
}
if(!$this->link || !@mysql_select_db($this->dsn['database'], $this->link)){
$this->halt();
}
}
return $this->link;
} //
// 提交SQL查询,返回执行后的结果集
// 根据$this->cfg_debug开关打印查询语句以及执行时间
//
function query($SQL)
{
$this->queryNo++;
if(!empty($SQL) && $this->connect()){
$time_start = $this->getMicroTime();
$result = @mysql_query($SQL);
$time_end = $this->getMicroTime();
if($this->cfg_debug){
//若有Debug参数,打印出SQL的相关信息。
$exeTime = round($time_end - $time_start, 6);
$SQL = htmlspecialchars($SQL);
print("Query:{$this->queryNo} Exec:{$exeTime}(S) SQL:{$SQL}<BR />");
}
if(!$result)
if($this->cfg_debug) $this->halt($SQL);
else $this->halt("无");
if($result === TRUE) return true;
$this->result = & new DB_result($result, $this->cfg_fetch);
}
return $this->result;
} //
// 关闭数据库连接
// pconnect打开的持续连接最好也执行close
//
function close()
{
if($this->link != NULL) @mysql_close($this->link);
} function setFetchMode($fetchMode)
{
$this->cfg_fetch = $fetchMode ? $fetchMode : $this->cfg_fetch;
} //
// 返回autoincrement的生成的值
//
function insertId()
{
return @mysql_insert_id($this->link);
}
//
// 返回上次SQL查询影响的行数
//
function affectedRows()
{
return @mysql_affected_rows($this->link);
} //
// 取得查询次数统计
//
function getQueryNo()
{
return $this->queryNo;
} //
// 记录可能产生的错误
//
function logErr()
{
$this->errNo = @mysql_errno($this->link);
$this->errMsg = @mysql_error($this->link);
} //
// 数据库出错后的处理函数
// 根据$this->cfg_haltOnErr开关决定是否显示错误报告并挂起程序
//
function halt($msg = "")
{
if($this->cfg_debug)
ob_end_flush();
else
ob_end_clean();
$time = date('Y-m-d H:i:s');
$error = @mysql_error();
$errno = @mysql_errno();
$page = $_SERVER['PHP_SELF'];
$content = DB_ERROR;
$content = str_replace("{copyrights}", $this->copyRights, $content);
$content = str_replace("{version}", $this->version, $content);
$content = str_replace("{error}", $error, $content);
$content = str_replace("{errno}", $errno, $content);
$content = str_replace("{time}", $time, $content);
$content = str_replace("{page}", $page, $content);
$content = str_replace("{msg}", $msg, $content);
print($content);
$this->logErr();
exit();
} function getMicroTime(){
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
} function parseDSN($dsn)
{ $parsed = array(
'protocol' => "",
'username' => "",
'password' => "",
'host' => "",
'port' => "",
'database' => "",
); // Find protocol
// $dsn => protocol://username:password@host:port/database
if (($pos = strpos($dsn, '://')) !== false) {
$parsed['protocol'] = substr($dsn, 0, $pos);
$dsn = substr($dsn, $pos + 3);
} else {
return $parsed;
} // Get (if found): username and password
// $dsn => username:password@host:port/database
if (($at = strrpos($dsn,'@')) !== false) {
$str = substr($dsn, 0, $at);
$dsn = substr($dsn, $at + 1);
if (($pos = strpos($str, ':')) !== false) {
$parsed['username'] = rawurldecode(substr($str, 0, $pos));
$parsed['password'] = rawurldecode(substr($str, $pos + 1));
} else {
$parsed['username'] = rawurldecode($str);
}
} // Get (if found): host and port
// $dsn => host:port/database
if (($at = strrpos($dsn, '/')) !== false) {
$str = substr($dsn, 0, $at);
$dsn = substr($dsn, $at + 1);
if (($pos = strpos($str, ':')) != false) {
$parsed['host'] = rawurldecode(substr($str, 0, $pos));
$parsed['port'] = rawurldecode(substr($str, $pos + 1));
} else {
$parsed['host'] = rawurldecode($str);
}
} // Get dabase if any
// $dsn => database
if ($dsn) {
$parsed['database'] = rawurldecode($dsn);
} return $parsed; }}class DB_result
{ var $result = NULL;
var $fetchMode = NULL; function DB_result($result, $fetchMode = DB_FETCHMODE_ASSOC)
{
$this->result = $result;
$this->fetchMode = $fetchMode;
} //
// 取得结果集中的下一条数据
// 正常取出返回true
// 数据已取完返回false
//
function fetchRow()
{
$record = NULL;
if($this->result){
switch($this->fetchMode){
case DB_FETCHMODE_ROW : $record = @mysql_fetch_row($this->result); break;
case DB_FETCHMODE_ASSOC : $record = @mysql_fetch_assoc($this->result); break;
case DB_FETCHMODE_ARRAY : $record = @mysql_fetch_array($this->result); break;
case DB_FETCHMODE_OBJECT : $record = @mysql_fetch_array($this->result); break;
}
if(!is_array($record)) $this->free();
}
return $record;
} //
// 返回结果集中的数据行数
//
function numRows()
{
if($this->result) return @mysql_num_rows($this->result);
else return 0;
} //
// 释放结果集
//
function free()
{
if($this->result != NULL){
@mysql_free_result($this->result);
$this->result = NULL;
}
}}define('DB_FETCHMODE_ROW', 1);
define('DB_FETCHMODE_ASSOC', 2);
define('DB_FETCHMODE_ARRAY', 3);
define('DB_FETCHMODE_OBJECT', 4);
define('DB_ERROR', "
<html>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=gb2312'>
<title>Database Error</title>
</head>
<style> P, TEXTAREA, BODY { FONT-FAMILY:Verdana, Arial, Helvetica; FONT-SIZE:9pt; } </style>
<body>
<blockquote>
<p> </p>
<p><strong>数据库好象发生了一些微小的错误.</strong><br><br>
请按浏览器的 <a href='javascript:window.location=window.location;'>刷新</a> 按钮重试</p><br>
<p>给您带来不便与困扰,我们着实深感抱歉...</p>
<textarea rows='15' cols='60'>数据库错误报告来自于 {copyrights} {version}错误信息:{error}错误号码:{errno}时间:{time}
页面:{page}
附注:{msg}
</textarea>
</blockquote>
</body>
</html>
");?>
其实我想要的是像我上面那段代码的功能,有谁有吗?或者能否帮我修改成那样的代码啊??我现在急用,由于对这个不熟悉,所以我一点思路都没有,谢谢各位了先!!!
$dbconn2 = pg_connect("host=localhost port=5432 dbname=mary");
但在PEAR::DB中的写法我记不得了,我要回家看了才知道
$dsn = pgsql://username:password@localhost:5432/database