热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

PostgreSQLDB简单操作类-PHP源码

PostgresqlDB简单操作类只用来执行SQL的其它的不懂
Postgresql DB 简单操作类

只用来执行SQL的 其它的不懂

//使用

define('DB_NAME', 'aaaa');// 数据库名
define('DB_USER', 'root');// 数据库用户
define('DB_PASSWORD', '123456');//数据库密码
define('DB_HOST', 'localhost');// 服务器名或服务器ip,一般为localhost
define('DB_CHARSET', 'utf8');//PostgreSQL编码设置.如果您的程序出现乱码现象,需要设置此项来修复. 请不要随意更改此项,否则将可能导致系统出现乱码现象
define('DB_PREFIX', 'icms_');// 表名前缀, 同一数据库安装多个请修改此处
define('DB_PREFIX_TAG', '#iCMS@__');// SQL表名前缀替换


iDB::query(sql);
iDB::getValue(sql);
iDB::getRow(sql);
iDB::getArray(sql);

1.iPgsql.class.php


* @site http://www.iiiphp.com
* @licence http://www.iiiphp.com/license
* @version 1.0.1
* @package iDB
* @$Id: iPgsql.class.php 44 2012-03-30 04:02:10Z coolmoo $
*/
define('DB_PORT', 5432);
define('OBJECT', 'OBJECT', true);
define('ARRAY_A', 'ARRAY_A', false);
define('ARRAY_N', 'ARRAY_N', false);
 
defined('SAVEQUERIES') OR define('SAVEQUERIES', true);
 
class iDB{
    public static $show_errors = false;
    public static $num_queries = 0;
    public static $last_query;
    public static $col_info;
    public static $queries;
    public static $func_call;
    public static $last_result;
    public static $num_rows;
    public static $insert_id;
 
    private static $collate;
    private static $time_start;
    private static $last_error ;
    private static $dbh;
    private static $result;
 
    function __construct() {
        if (!self::$dbh)
            self::connect();
    }
    function connect() {
        extension_loaded('pgsql') OR die('您的 PHP 安装看起来缺少 PostgreSQL 数据库部分,
        这对 iPHP 来说是必须的。');
         
        defined('DB_COLLATE') && self::$collate = DB_COLLATE;
 
        self::$dbh = pg_connect("host=".DB_HOST." port=".DB_PORT." dbname=".DB_NAME.
        " user=".DB_USER." password=".DB_PASSWORD);
 
        defined('DB_CHARSET') && self::query("set client_encoding to '".DB_CHARSET."'");
 
        self::$dbh OR self::bail("

数据库链接失败

请检查 config.php 的配置是否正确!

  • 请确认主机支持PostgreSQL?
  • 请确认用户名和密码正确?
  • 请确认主机名正确?(一般为localhost)

如果你不确定这些情况,请询问你的主机提供商. 如果你还需要帮助你可以随时浏览 iPHP 支持论坛.

"); //@mysql_select_db(DB_NAME, self::$dbh) OR self::bail("

链接到".DB_NAME."数据库失败

我们能连接到数据库服务器(即数据库用户名和密码正确) ,但是不能链接到$db数据库.

  • 你确定$db存在?

如果你不确定这些情况,请询问你的主机提供商.如果你还需要帮助你可以随时浏览 iPHP 支持论坛.

"); } // ================================================================== // Print SQL/DB error. function print_error($str = '') { if (!$str) $str = pg_result_error(self::$dbh); $EZSQL_ERROR[] = array ('query' => self::$last_query, 'error_str' => $str); $str = htmlspecialchars($str, ENT_QUOTES); $query = htmlspecialchars(self::$last_query, ENT_QUOTES); // Is error output turned on or not.. if ( self::$show_errors ) { // If there is an error then take note of it die("

iPHP database error: [$str]
$query

"); } else { return false; } } // ================================================================== // Kill cached query results function flush() { self::$last_result = array(); self::$col_info = null; self::$last_query = null; } // ================================================================== // Basic Query - see docs for more detail function query($query,$QT=NULL) { if (!self::$dbh) { self::connect(); } // filter the query, if filters are available // NOTE: some queries are made before the plugins have been loaded, and thus cannot be filtered with this method $query=str_replace(DB_PREFIX_TAG,DB_PREFIX, $query); // initialise return $return_val = 0; self::flush(); // Log how the function was called self::$func_call = __CLASS__."::query(\"$query\")"; // Keep track of the last query for debug.. self::$last_query = $query; // Perform the query via std pgsql_query function.. if (SAVEQUERIES) self::timer_start(); self::$result = pg_query(self::$dbh,$query); self::$num_queries++; if (SAVEQUERIES) self::$queries[] = array( $query, self::timer_stop()); // If there is an error then take note of it.. if ( self::$last_error = pg_result_error(self::$result) ) { self::print_error(); return false; } $QH = strtoupper(substr($query,0,strpos($query, ' '))); if (in_array($QH,array("INSERT","DELETE","UPDATE","REPLACE"))) { $rows_affected = pg_affected_rows (self::$result); // Take note of the insert_id if (in_array($QH,array("INSERT","REPLACE"))) { self::$insert_id = pg_last_oid(self::$result); } // Return number of rows affected $return_val = $rows_affected; } else { if($QT=="field") { $i = 0; while ($i value relationships. Multiple member pairs will be joined with ANDs. WARNING: the column names are not currently sanitized! * @return mixed results of self::query() */ function update($table, $data, $where) { // $data = add_magic_quotes($data); $bits = $wheres = array(); foreach ( array_keys($data) as $k ) $bits[] = "`$k` = '$data[$k]'"; if ( is_array( $where ) ) foreach ( $where as $c => $v ) $wheres[] = "$c = '" . addslashes( $v ) . "'"; else return false; return self::query("UPDATE ".DB_PREFIX_TAG."{$table} SET " . implode( ', ', $bits ) . ' WHERE ' . implode( ' AND ', $wheres ) . ' LIMIT 1' ); } /** * Get one variable from the database * @param string $query (can be null as well, for caching, see codex) * @param int $x = 0 row num to return * @param int $y = 0 col num to return * @return mixed results */ function getValue($query=null, $x = 0, $y = 0) { self::$func_call = __CLASS__."::getValue(\"$query\",$x,$y)"; if ( $query ) self::query($query); // Extract var out of cached results based x,y vals if ( !empty( self::$last_result[$y] ) ) { $values = array_values(get_object_vars(self::$last_result[$y])); } // If there is a value return it else return null return (isset($values[$x]) && $values[$x]!=='') ? $values[$x] : null; } /** * Get one row from the database * @param string $query * @param string $output ARRAY_A | ARRAY_N | OBJECT * @param int $y row num to return * @return mixed results */ function getRow($query = null, $output = OBJECT, $y = 0) { self::$func_call = __CLASS__."::getRow(\"$query\",$output,$y)"; if ( $query ) self::query($query); if ( !isset(self::$last_result[$y]) ) return null; if ( $output == OBJECT ) { return self::$last_result[$y] ? self::$last_result[$y] : null; } elseif ( $output == ARRAY_A ) { return self::$last_result[$y] ? get_object_vars(self::$last_result[$y]) : null; } elseif ( $output == ARRAY_N ) { return self::$last_result[$y] ? array_values(get_object_vars(self::$last_result[$y])) : null; } else { self::print_error(__CLASS__."::getRow(string query, output type, int offset) -- Output type must be one of: OBJECT, ARRAY_A, ARRAY_N"); } } /** * Return an entire result set from the database * @param string $query (can also be null to pull from the cache) * @param string $output ARRAY_A | ARRAY_N | OBJECT * @return mixed results */ function getArray($query = null, $output = ARRAY_A) { self::$func_call = __CLASS__."::getArray(\"$query\", $output)"; if ( $query ) self::query($query); // Send back array of objects. Each row is an object if ( $output == OBJECT ) { return self::$last_result; } elseif ( $output == ARRAY_A || $output == ARRAY_N ) { if ( self::$last_result ) { $i = 0; foreach( (array) self::$last_result as $row ) { if ( $output == ARRAY_N ) { // ...integer-keyed row arrays $new_array[$i] = array_values( get_object_vars( $row ) ); } else { // ...column name-keyed row arrays $new_array[$i] = get_object_vars( $row ); } ++$i; } return $new_array; } else { return null; } } } /** * Gets one column from the database * @param string $query (can be null as well, for caching, see codex) * @param int $x col num to return * @return array results */ function getCol($query = null , $x = 0) { if ( $query ) self::query($query); $new_array = array(); // Extract the column values for ( $i=0; $i {$info_type}; $i++; } return $new_array; } else { return self::$col_info[$col_offset]->{$info_type}; } } } function version() { // Make sure the server has PostgreSQL 4.0 $v = pg_version(self::$dbh); return $v['client']; } /** * Starts the timer, for debugging purposes */ function timer_start() { $mtime = microtime(); $mtime = explode(' ', $mtime); self::$time_start = $mtime[1] + $mtime[0]; return true; } /** * Stops the debugging timer * @return int total time spent on the query, in milliseconds */ function timer_stop() { $mtime = microtime(); $mtime = explode(' ', $mtime); $time_end = $mtime[1] + $mtime[0]; $time_total = $time_end - self::$time_start; return $time_total; } /** * Wraps fatal errors in a nice header and footer and dies. * @param string $message */ function bail($message){ // Just wraps errors in a nice header and footer if ( !self::$show_errors ) { return false; } header('Content-Type: text/html; charset=utf8'); echo '

iPHP

'.$message.'

'; exit(); } }


以上就是PostgreSQL DB 简单操作类的内容,更多相关内容请关注PHP中文网(www.php1.cn)!

推荐阅读
  • 在说Hibernate映射前,我们先来了解下对象关系映射ORM。ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对 ... [详细]
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • 如何实现织梦DedeCms全站伪静态
    本文介绍了如何通过修改织梦DedeCms源代码来实现全站伪静态,以提高管理和SEO效果。全站伪静态可以避免重复URL的问题,同时通过使用mod_rewrite伪静态模块和.htaccess正则表达式,可以更好地适应搜索引擎的需求。文章还提到了一些相关的技术和工具,如Ubuntu、qt编程、tomcat端口、爬虫、php request根目录等。 ... [详细]
  • 本文介绍了如何使用php限制数据库插入的条数并显示每次插入数据库之间的数据数目,以及避免重复提交的方法。同时还介绍了如何限制某一个数据库用户的并发连接数,以及设置数据库的连接数和连接超时时间的方法。最后提供了一些关于浏览器在线用户数和数据库连接数量比例的参考值。 ... [详细]
  • 本文介绍了在SpringBoot中集成thymeleaf前端模版的配置步骤,包括在application.properties配置文件中添加thymeleaf的配置信息,引入thymeleaf的jar包,以及创建PageController并添加index方法。 ... [详细]
  • 搭建Windows Server 2012 R2 IIS8.5+PHP(FastCGI)+MySQL环境的详细步骤
    本文详细介绍了搭建Windows Server 2012 R2 IIS8.5+PHP(FastCGI)+MySQL环境的步骤,包括环境说明、相关软件下载的地址以及所需的插件下载地址。 ... [详细]
  • 本文介绍了使用AJAX的POST请求实现数据修改功能的方法。通过ajax-post技术,可以实现在输入某个id后,通过ajax技术调用post.jsp修改具有该id记录的姓名的值。文章还提到了AJAX的概念和作用,以及使用async参数和open()方法的注意事项。同时强调了不推荐使用async=false的情况,并解释了JavaScript等待服务器响应的机制。 ... [详细]
  • PHP设置MySQL字符集的方法及使用mysqli_set_charset函数
    本文介绍了PHP设置MySQL字符集的方法,详细介绍了使用mysqli_set_charset函数来规定与数据库服务器进行数据传送时要使用的字符集。通过示例代码演示了如何设置默认客户端字符集。 ... [详细]
  • 本文讨论了Alink回归预测的不完善问题,指出目前主要针对Python做案例,对其他语言支持不足。同时介绍了pom.xml文件的基本结构和使用方法,以及Maven的相关知识。最后,对Alink回归预测的未来发展提出了期待。 ... [详细]
  • Oracle Database 10g许可授予信息及高级功能详解
    本文介绍了Oracle Database 10g许可授予信息及其中的高级功能,包括数据库优化数据包、SQL访问指导、SQL优化指导、SQL优化集和重组对象。同时提供了详细说明,指导用户在Oracle Database 10g中如何使用这些功能。 ... [详细]
  • 知识图谱——机器大脑中的知识库
    本文介绍了知识图谱在机器大脑中的应用,以及搜索引擎在知识图谱方面的发展。以谷歌知识图谱为例,说明了知识图谱的智能化特点。通过搜索引擎用户可以获取更加智能化的答案,如搜索关键词"Marie Curie",会得到居里夫人的详细信息以及与之相关的历史人物。知识图谱的出现引起了搜索引擎行业的变革,不仅美国的微软必应,中国的百度、搜狗等搜索引擎公司也纷纷推出了自己的知识图谱。 ... [详细]
  • 本文由编程笔记小编整理,介绍了PHP中的MySQL函数库及其常用函数,包括mysql_connect、mysql_error、mysql_select_db、mysql_query、mysql_affected_row、mysql_close等。希望对读者有一定的参考价值。 ... [详细]
  • 本文介绍了使用PHP实现断点续传乱序合并文件的方法和源码。由于网络原因,文件需要分割成多个部分发送,因此无法按顺序接收。文章中提供了merge2.php的源码,通过使用shuffle函数打乱文件读取顺序,实现了乱序合并文件的功能。同时,还介绍了filesize、glob、unlink、fopen等相关函数的使用。阅读本文可以了解如何使用PHP实现断点续传乱序合并文件的具体步骤。 ... [详细]
  • Oracle分析函数first_value()和last_value()的用法及原理
    本文介绍了Oracle分析函数first_value()和last_value()的用法和原理,以及在查询销售记录日期和部门中的应用。通过示例和解释,详细说明了first_value()和last_value()的功能和不同之处。同时,对于last_value()的结果出现不一样的情况进行了解释,并提供了理解last_value()默认统计范围的方法。该文对于使用Oracle分析函数的开发人员和数据库管理员具有参考价值。 ... [详细]
  • 本文介绍了在Mac上搭建php环境后无法使用localhost连接mysql的问题,并通过将localhost替换为127.0.0.1或本机IP解决了该问题。文章解释了localhost和127.0.0.1的区别,指出了使用socket方式连接导致连接失败的原因。此外,还提供了相关链接供读者深入了解。 ... [详细]
author-avatar
麻廿_965
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有