Ejemplo de una clase sencilla para mysql, tomada de
PunBB:
Código php:
Ver original<?php
/***********************************************************************
This file is part of PunBB.
PunBB is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published
by the Free Software Foundation; either version 2 of the License,
or (at your option) any later version.
PunBB is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston,
MA 02111-1307 USA
************************************************************************/
header('HTTP/1.0 403 Forbidden'); die('You don\'t have permission to browse this directory or open the file.'); }
// Make sure we have built in support for MySQL
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.');
class DBLayer {
var $prefix;
var $link_id;
var $query_result;
var $saved_queries = array(); var $show_queries = false;
var $num_queries = 0;
function DBLayer($db_host, $db_username, $db_password, $db_name, $db_prefix, $p_connect, $showq = false) {
$this->prefix = $db_prefix;
$this->show_queries = $showq;
if ($p_connect)
$this->link_id = @mysql_pconnect($db_host, $db_username, $db_password); else
$this->link_id = @mysql_connect($db_host, $db_username, $db_password);
if ($this->link_id) {
return $this->link_id;
error
('Unable to select database. MySQL reported: '.mysql_error(), __FILE__, __LINE__); }
error
('Unable to connect to MySQL server. MySQL reported: '.mysql_error(), __FILE__, __LINE__); }
function start_transaction() {
return;
}
function end_transaction() {
return;
}
function query($sql, $check_empty = false, $unbuffered = false) {
if ($this->show_queries)
if ($unbuffered)
else
$this->query_result = @mysql_query($sql, $this->link_id); if ($this->query_result) {
if ($this->show_queries)
++$this->num_queries;
// If check_empty = true and there were no rows loaded, return false
if($check_empty && $this->num_rows($this->query_result) < 1)
return false;
return $this->query_result;
}
if ($this->show_queries)
$this->saved_queries[] = array($sql, 0); return false;
}
function result($query_id = 0, $row = 0) {
return ($query_id) ?
@mysql_result($query_id, $row) : false; }
function fetch_assoc($query_id = 0) {
}
function fetch_row($query_id = 0) {
}
function num_rows($query_id = 0) {
}
function affected_rows() {
}
function insert_id() {
}
function get_num_queries() {
return $this->num_queries;
}
function get_saved_queries() {
return $this->saved_queries;
}
function free_result($query_id = false) {
}
function escape($str) {
return '';
else
return $str;
}
function error() {
$result['error_sql'] = @current(@end($this->saved_queries)); return $result;
}
function close() {
if ($this->link_id) {
if ($this->query_result)
}
return false;
}
}