punbb/include/dblayer/mysql.php
author Dan
Wed, 11 Jul 2007 21:01:48 -0400
changeset 0 f9ffdbd96607
permissions -rw-r--r--
Initial population
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
     1
<?php
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
     2
/***********************************************************************
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
     3
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
     4
  Copyright (C) 2002-2005  Rickard Andersson (rickard@punbb.org)
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
     5
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
     6
  This file is part of PunBB.
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
     7
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
     8
  PunBB is free software; you can redistribute it and/or modify it
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
     9
  under the terms of the GNU General Public License as published
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    10
  by the Free Software Foundation; either version 2 of the License,
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    11
  or (at your option) any later version.
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    12
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    13
  PunBB is distributed in the hope that it will be useful, but
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    14
  WITHOUT ANY WARRANTY; without even the implied warranty of
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    15
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    16
  GNU General Public License for more details.
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    17
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    18
  You should have received a copy of the GNU General Public License
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    19
  along with this program; if not, write to the Free Software
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    20
  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    21
  MA  02111-1307  USA
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    22
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    23
************************************************************************/
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    24
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    25
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    26
// Make sure we have built in support for MySQL
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    27
if (!function_exists('mysql_connect'))
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    28
	exit('This PHP environment doesn\'t have MySQL support built in. MySQL support is required if you want to use a MySQL database to run this forum. Consult the PHP documentation for further assistance.');
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    29
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    30
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    31
class DBLayer
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    32
{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    33
	var $prefix;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    34
	var $link_id;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    35
	var $query_result;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    36
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    37
	var $saved_queries = array();
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    38
	var $num_queries = 0;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    39
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    40
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    41
	function DBLayer($db_host, $db_username, $db_password, $db_name, $db_prefix, $p_connect)
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    42
	{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    43
		$this->prefix = $db_prefix;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    44
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    45
		if ($p_connect)
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    46
			$this->link_id = @mysql_pconnect($db_host, $db_username, $db_password);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    47
		else
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    48
			$this->link_id = @mysql_connect($db_host, $db_username, $db_password);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    49
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    50
		if ($this->link_id)
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    51
		{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    52
			if (@mysql_select_db($db_name, $this->link_id))
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    53
				return $this->link_id;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    54
			else
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    55
				error('Unable to select database. MySQL reported: '.mysql_error(), __FILE__, __LINE__);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    56
		}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    57
		else
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    58
			error('Unable to connect to MySQL server. MySQL reported: '.mysql_error(), __FILE__, __LINE__);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    59
	}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    60
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    61
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    62
	function start_transaction()
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    63
	{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    64
		return;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    65
	}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    66
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    67
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    68
	function end_transaction()
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    69
	{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    70
		return;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    71
	}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    72
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    73
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    74
	function query($sql, $unbuffered = false)
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    75
	{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    76
		if (defined('PUN_SHOW_QUERIES'))
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    77
			$q_start = get_microtime();
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    78
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    79
		if ($unbuffered)
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    80
			$this->query_result = @mysql_unbuffered_query($sql, $this->link_id);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    81
		else
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    82
			$this->query_result = @mysql_query($sql, $this->link_id);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    83
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    84
		if ($this->query_result)
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    85
		{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    86
			if (defined('PUN_SHOW_QUERIES'))
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    87
				$this->saved_queries[] = array($sql, sprintf('%.5f', get_microtime() - $q_start));
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    88
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    89
			++$this->num_queries;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    90
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    91
			return $this->query_result;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    92
		}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    93
		else
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    94
		{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    95
			if (defined('PUN_SHOW_QUERIES'))
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    96
				$this->saved_queries[] = array($sql, 0);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    97
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    98
			return false;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
    99
		}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   100
	}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   101
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   102
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   103
	function result($query_id = 0, $row = 0)
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   104
	{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   105
		return ($query_id) ? @mysql_result($query_id, $row) : false;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   106
	}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   107
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   108
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   109
	function fetch_assoc($query_id = 0)
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   110
	{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   111
		return ($query_id) ? @mysql_fetch_assoc($query_id) : false;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   112
	}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   113
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   114
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   115
	function fetch_row($query_id = 0)
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   116
	{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   117
		return ($query_id) ? @mysql_fetch_row($query_id) : false;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   118
	}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   119
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   120
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   121
	function num_rows($query_id = 0)
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   122
	{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   123
		return ($query_id) ? @mysql_num_rows($query_id) : false;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   124
	}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   125
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   126
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   127
	function affected_rows()
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   128
	{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   129
		return ($this->link_id) ? @mysql_affected_rows($this->link_id) : false;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   130
	}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   131
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   132
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   133
	function insert_id()
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   134
	{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   135
		return ($this->link_id) ? @mysql_insert_id($this->link_id) : false;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   136
	}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   137
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   138
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   139
	function get_num_queries()
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   140
	{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   141
		return $this->num_queries;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   142
	}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   143
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   144
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   145
	function get_saved_queries()
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   146
	{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   147
		return $this->saved_queries;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   148
	}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   149
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   150
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   151
	function free_result($query_id = false)
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   152
	{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   153
		return ($query_id) ? @mysql_free_result($query_id) : false;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   154
	}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   155
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   156
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   157
	function escape($str)
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   158
	{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   159
		if (is_array($str))
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   160
			return '';
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   161
		else if (function_exists('mysql_real_escape_string'))
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   162
			return mysql_real_escape_string($str, $this->link_id);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   163
		else
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   164
			return mysql_escape_string($str);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   165
	}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   166
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   167
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   168
	function error()
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   169
	{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   170
		$result['error_sql'] = @current(@end($this->saved_queries));
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   171
		$result['error_no'] = @mysql_errno($this->link_id);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   172
		$result['error_msg'] = @mysql_error($this->link_id);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   173
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   174
		return $result;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   175
	}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   176
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   177
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   178
	function close()
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   179
	{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   180
		if ($this->link_id)
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   181
		{
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   182
			if ($this->query_result)
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   183
				@mysql_free_result($this->query_result);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   184
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   185
			return @mysql_close($this->link_id);
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   186
		}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   187
		else
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   188
			return false;
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   189
	}
f9ffdbd96607 Initial population
Dan
parents:
diff changeset
   190
}