⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ezsql_postgres.php

📁 网页留言本,比一般的留言簿管用
💻 PHP
📖 第 1 页 / 共 2 页
字号:
<?php	// ==================================================================	//  Author: Justin Vincent (justin@visunet.ie)	//	Web: 	http://php.justinvincent.com	//	Name: 	ezSQL	// 	Desc: 	Class to make it very easy to deal with PostgreSQL database connections.	//	//	N.B. ezSQL was converted for use with PostgreSQL 	//	     by Michael Paesold (mpaesold@gmx.at).	//       and then reworked to act more like mysql version by 	//       Tom De Bruyne (tom@morefast.be)	//	// !! IMPORTANT !!	//	//  Please send me a mail telling me what you think of ezSQL	//  and what your using it for!! Cheers. [ justin@visunet.ie ]	//	// ==================================================================	// User Settings -- CHANGE HERE	define("EZSQL_DB_USER", "");		// <-- PostgreSQL db user	define("EZSQL_DB_PASSWORD", "");	// <-- PostgreSQL db password	define("EZSQL_DB_NAME", "");		// <-- PostgreSQL db pname	define("EZSQL_DB_HOST", "");		// <-- PostgreSQL server host	// ==================================================================	//	ezSQL Constants	define("EZSQL_VERSION","1.26");	define("OBJECT","OBJECT",true);	define("ARRAY_A","ARRAY_A",true);	define("ARRAY_N","ARRAY_N",true);	// ==================================================================	//	The Main Class	class ezsql {		var $debug_called;		var $vardump_called;		var $show_errors = true;		var $num_queries = 0;		var $debug_all = false;		var $last_query;		var $col_info;				// ==================================================================		//	DB Constructor - connects to the server and selects a database		function db($dbuser, $dbpassword, $dbname, $dbhost)		{			$connect_str = "";			if (! empty($dbhost)) $connect_str .= " host=$dbhost";			if (! empty($dbname)) $connect_str .= " dbname=$dbname";			if (! empty($dbuser)) $connect_str .= " user=$dbuser";			if (! empty($dbpassword)) $connect_str .= " password=$dbpassword";				$this->dbh = @pg_connect($connect_str);				if ( ! $this->dbh )			{				$this->print_error("<ol><b>Error establishing a database connection!</b><li>Are you sure you have the correct user/password?<li>Are you sure that you have typed the correct hostname?<li>Are you sure that the database server is running?<li></ol>");			}			else			{				// Remember these values for the select function				$this->dbuser = $dbuser;				$this->dbpassword = $dbpassword;				$this->dbname = $dbname;				$this->dbhost = $dbhost;			}		}		// ==================================================================		//	Select a DB (if another one needs to be selected)		function select($db)		{			$this->db($this->dbuser, $this->dbpassword, $db, $this->dbhost);		}		// ====================================================================		//	Format a string correctly for safe insert under all PHP conditions				function escape($str)		{			return pg_escape_string(stripslashes($str));						}		// ==================================================================		//	Print SQL/DB error.			function print_error($str = "")		{				// All erros go to the global error array $EZSQL_ERROR..			global $EZSQL_ERROR;				// If no special error string then use mysql default..			if ( !$str ) $str = pg_last_error();				// Log this error to the global array..			$EZSQL_ERROR[] = array 							(								"query" => $this->last_query,								"error_str"  => $str							);				// Is error output turned on or not..			if ( $this->show_errors )			{				// If there is an error then take note of it				print "<blockquote><font face=arial size=2 color=ff0000>";				print "<b>SQL/DB Error --</b> ";				print "[<font color=000077>$str</font>]";				print "</font></blockquote>";			}			else			{				return false;				}		}		// ==================================================================		//	Turn error handling on or off..			function show_errors()		{			$this->show_errors = true;		}			function hide_errors()		{			$this->show_errors = false;		}		// ==================================================================		//	Kill cached query results			function flush()		{				// Get rid of these			$this->last_result = null;			$this->col_info = null;			$this->last_query = null;			}		// ==================================================================		//	Try to get last ID		function get_insert_id($query) 		{						$this->last_oid = pg_last_oid($this->result);											// try to find table name								eregi ("insert *into *([^ ]+).*", $query, $regs);								//print_r($regs);								$table_name = $regs[1];					$query_for_id = "SELECT * FROM $table_name WHERE oid='$this->last_oid'";								//echo $query_for_id."<br>";								$result_for_id = pg_query($this->dbh, $query_for_id);								if(pg_num_rows($result_for_id)) {					$id = pg_fetch_array($result_for_id,0,PGSQL_NUM);									//print_r($id);									return $id[0];				}		}		// ==================================================================		//	Basic Query	- see docs for more detail			function query($query)		{				// For reg expressions			$query = trim($query); 						// Flush cached values..			$this->flush();				// Log how the function was called			$this->func_call = "\$db->query(\"$query\")";				// Keep track of the last query for debug..			$this->last_query = $query;				// Perform the query via std pg_query function..			if (!$this->result = @pg_query($this->dbh,$query)) {				$this->print_error();				return false;			}						$this->num_queries++;				// If there was an insert, delete or update see how many rows were affected			// (Also, If there there was an insert take note of the last OID			$query_type = array("insert","delete","update","replace");				// loop through the above array			foreach ( $query_type as $word )			{				// This is true if the query starts with insert, delete or update				if ( preg_match("/^$word\s+/i",$query) )				{					$this->rows_affected = pg_affected_rows($this->result);						// This gets the insert ID					if ( $word == "insert" )					{												$this->insert_id = $this->get_insert_id($query);												// If insert id then return it - true evaluation						return $this->insert_id;					}										// Set to false if there was no insert id					$this->result = false;									}			}				// In other words if this was a select statement..			if ( $this->result )			{					// =======================================================				// Take note of column info					$i=0;				while ($i < @pg_num_fields($this->result))				{					$this->col_info[$i]->name = pg_field_name($this->result,$i);					$this->col_info[$i]->type = pg_field_type($this->result,$i);					$this->col_info[$i]->size = pg_field_size($this->result,$i);					$i++;				}					// =======================================================				// Store Query Results					$i=0;				while ( $row = @pg_fetch_object($this->result, $i, PGSQL_ASSOC) )				{					// Store relults as an objects within main array					$this->last_result[$i] = $row;						$i++;				}					// Log number of rows the query returned				$this->num_rows = $i;					@pg_free_result($this->result);				// If debug ALL queries				$this->debug_all ? $this->debug() : null ;				// If there were results then return true for $db->query				if ( $i )				{					return true;				}				else				{					return false;				}			}			else			{				// If debug ALL queries				$this->debug_all ? $this->debug() : null ;									// Update insert etc. was good..				return true;			}				}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -