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