This is a little class for making mysql functions a little easier. I have now half written a
new version of this class for the new PEAR::MDB2 API but just now this makes doing things with
mySql a little quicker
<?PHP
/*
#Wrapper for the PEAR::DB conecting to mySQL
#Copyright (C) 2006 Steven McCullie
#
#This library is free software; you can redistribute it and/or
#modify it under the terms of the GNU Lesser General Public
#License as published by the Free Software Foundation; either
#version 2.1 of the License, or (at your option) any later version.
#
#This library 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
#Lesser General Public License for more details.
#
#You should have received a copy of the GNU Lesser General Public
#License along with this library; if not, write to the Free Software
#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#
# mysql.class.php
# This file wraps the basic mySQL
# function using the PEAR:DB class
# making mySQL quicker and easier to work with
# we also put the server details to better secure
# the site
# Steven@orinett.com
*/
class mysql
{
function _sqlConnect()
{
require_once 'DB.php';
$dsn = array(
'phptype' => 'mysql',
'username' => '',
'password' => '',
'hostspec' => '',
'database' => '',
);
$options = array(
'debug' => 2,
'portability' => DB_PORTABILITY_ALL,
);
$db =& DB::connect($dsn, $options);
if (PEAR::isError($db)) {
die($db->getMessage());
}
$db->setFetchMode(DB_FETCHMODE_ASSOC);
return($db);
//end function
}
function sqlSelect($tables,$columns,$conditions)
{
$db = $this->_sqlConnect();
// Proceed with a query...
$sql = 'SELECT '.$columns.' FROM '.$tables.' '.$conditions;
// Once you have a valid DB object named $db...
$data =& $db->getAll($sql);
if (PEAR::isError($data))
{
die($data->getMessage());
}
return($data);
}
function _sqlInsert($tables,$arrValues)
{
$db = $this->_sqlConnect();
$res = $db->autoExecute($tables, $arrValues,
DB_AUTOQUERY_INSERT,'');
if (PEAR::isError($res)) {
die($res->getMessage());
}
//end of function
}
function sqlUpdate($tables,$arrValues,$conditions)
{
$db = $this->_sqlConnect();
$res = $db->autoExecute($tables, $arrValues,
DB_AUTOQUERY_UPDATE, ''.$conditions.'');
if (PEAR::isError($res)) {
die($res->getMessage());
}
//end of function
}
function sqlQuery($str)
{
$db = $this->_sqlConnect();
$res = $db->query($str);
if (PEAR::isError($res)) {
die($res->getMessage());
}
//end of function
}
//end of class
}
?>