PDA - personal digital assistant

Visualizza versione completa : Simple Database & Query Handling



Admin
13/06/2009, 19:08
<?php
/*================================================= ===========================*\
|| ################################################## ######################## ||
|| # Simple Database & Query Handling # ||
|| # ---------------------------------------------------------------------- # ||
|| # (C) Copyright Y2K Software s.a.s. 2009 - All Rights Reserved. # ||
|| # This file may not be redistributed in whole or significant part. # ||
|| ################################################## ######################## ||
\*================================================ ============================*/

/**
* @author Y2K Software
* @copyright (C) Copyright Y2K Software 2009 - All Rights Reserved
* @version 1.0.0
* @link www.pagerobot.com
*/

class db_mysql
{
var $host = 'localhost';
var $port = 3306;
var $user = 'root';
var $pass = '';
var $name = '';
var $error = '';
var $errno = 0;
var $SQL = '';

/**
* db_mysql::_seterror()
*
* Sets error codes.
*
* @return void
*/
function _seterror()
{
$this->errno = @mysql_errno();
$this->error = @mysql_error();
}

/**
* db_mysql::open()
*
* Opens a database.
*
* @return boolean success
*/
function open()
{
$this->close();

if(!$this->connect())
{
return false;
}
if(!@mysql_select_db($this->name))
{
return false;
}

return true;
}

/**
* db_mysql::connect()
*
* Connects to a database host.
*
* @return boolean success
*/
function connect()
{
$this->close();

if(!@mysql_connect($this->host . ':' . $this->port, $this->user, $this->pass))
{
$this->_seterror();
return false;
}

return true;
}

/**
* db_mysql::close()
*
* Closes a database.
*
* @return boolean success
*/
function close()
{
@mysql_close();

return true;
}

/**
* db_mysql::query_read()
*
* Opens a recordset for read.
*
* @param mixed $SQL
* @return result open recordset
*/
function query_read($SQL)
{
if(!$rss = @mysql_query($SQL))
{
$this->SQL = $SQL;
$this->_seterror();
return false;
}

return $rss;
}

/**
* db_mysql::query_write()
*
* Executes a query.
*
* @param mixed $SQL
* @return boolean success
*/
function query_write($SQL)
{
if(!@mysql_query($SQL))
{
$this->SQL = $SQL;
$this->_seterror();
return false;
}

return true;
}

/**
* db_mysql::free()
*
* Closes a recordset.
*
* @param mixed $rss
* @return boolean success
*/
function free($rss)
{
@mysql_free_result($rss);

return true;
}

/**
* db_mysql::query_first()
*
* Gets the first matching entry of a read query.
* Cleses the recordset.
*
* @param mixed $SQL
* @return result row
*/
function query_first($SQL)
{
if(!$rss = $this->query_read($SQL))
{
return false;
}

if(!$rs = $this->fetch($rss))
{
return false;
}

$this->free($rss);

return $rs;
}

/**
* db_mysql::fetch()
*
* Gets an associative row from a given recordset.
*
* @param mixed $rss
* @return result row
*/
function fetch($rss)
{
if(!$rs = @mysql_fetch_assoc($rss))
{
$this->_seterror();
return false;
}

return $rs;
}

/**
* db_mysql::fetch_row()
*
* Gets a nomerically indexed row from a given recordset.
*
* @param mixed $rss
* @return result row
*/
function fetch_row($rss)
{
if(!$rs = @mysql_fetch_row($rss))
{
$this->_seterror();
return false;
}

return $rs;
}

/**
* db_mysql::escape_string()
*
* Escapes a string for a query.
*
* @param mixed $string
* @return mixed $string
*/
function escape_string($string)
{
return mysql_real_escape_string($string);
}

/**
* db_mysql::rows()
*
* Returns the number of affected rows from the last query.
*
* @param mixed $rss
* @return number of rows
*/
function rows($rss)
{
$n = @mysql_num_rows($rss);
if($n === false)
{
$this->_seterror();
$n = 0;
}

return $n;
}
}
?>

Admin
02/09/2010, 14:24
<?php
/*================================================= ===========================*\
|| ################################################## ######################## ||
|| # Simple Database & Query Handling # ||
|| # ---------------------------------------------------------------------- # ||
|| # (C) Copyright Y2K Software s.a.s. 2009-2010 - All Rights Reserved. # ||
|| # This file may not be redistributed in whole or significant part. # ||
|| ################################################## ######################## ||
\*================================================ ============================*/

/**
* @author Y2K Software
* @copyright (C) Copyright Y2K Software 2009-10 - All Rights Reserved
* @version 1.0.1
* @link www.pagerobot.com
*/

class db_mysql
{
var $host = 'localhost';
var $port = 3306;
var $user = 'root';
var $pass = '';
var $name = '';
var $error = '';
var $errno = 0;
var $SQL = '';

/**
* db_mysql::_seterror()
*
* Sets error codes.
*
* @return void
*/
function _seterror()
{
$this->errno = @mysql_errno();
$this->error = @mysql_error();
}

/**
* db_mysql::open()
*
* Opens a database.
*
* @return boolean success
*/
function open()
{
$this->close();

if(!$this->connect())
{
return false;
}
if(!@mysql_select_db($this->name))
{
return false;
}

return true;
}

/**
* db_mysql::connect()
*
* Connects to a database host.
*
* @return boolean success
*/
function connect()
{
$this->close();

if(!@mysql_connect($this->host . ':' . $this->port, $this->user, $this->pass))
{
$this->_seterror();
return false;
}

return true;
}

/**
* db_mysql::close()
*
* Closes a database.
*
* @return boolean success
*/
function close()
{
@mysql_close();

return true;
}

/**
* db_mysql::query_read()
*
* Opens a recordset for read.
*
* @param mixed $SQL
* @return result open recordset
*/
function query_read($SQL)
{
if(!$rss = @mysql_query($SQL))
{
$this->SQL = $SQL;
$this->_seterror();
return false;
}

return $rss;
}

/**
* db_mysql::query_write()
*
* Executes a query.
*
* @param mixed $SQL
* @return boolean success
*/
function query_write($SQL)
{
if(!@mysql_query($SQL))
{
$this->SQL = $SQL;
$this->_seterror();
return false;
}

return true;
}

/**
* db_mysql::free()
*
* Closes a recordset.
*
* @param mixed $rss
* @return boolean success
*/
function free($rss)
{
@mysql_free_result($rss);

return true;
}

/**
* db_mysql::query_first()
*
* Gets the first matching entry of a read query.
* Cleses the recordset.
*
* @param mixed $SQL
* @return result row
*/
function query_first($SQL, $type = 0)
{
if(!$rss = $this->query_read($SQL))
{
return false;
}

if(!$rs = $this->fetch($rss, $type))
{
return false;
}

$this->free($rss);

return $rs;
}

/**
* db_mysql::fetch()
*
* Gets an associative row from a given recordset.
*
* @param mixed $rss
* @param integer $type
* @return result row
*/
function fetch($rss, $type = 0)
{
if($type == 0)
{
$rs = @mysql_fetch_assoc($rss);
}
else
{
$rs = @mysql_fetch_row($rss);
}

if(!$rs)
{
$this->_seterror();
return false;
}

return $rs;
}

/**
* db_mysql::fetch_row()
*
* Gets a nomerically indexed row from a given recordset.
*
* @param mixed $rss
* @return result row
*/
function fetch_row($rss)
{
if(!$rs = @mysql_fetch_row($rss))
{
$this->_seterror();
return false;
}

return $rs;
}

/**
* db_mysql::escape_string()
*
* Escapes a string for a query.
*
* @param mixed $string
* @return mixed $string
*/
function escape_string($string)
{
return mysql_real_escape_string($string);
}

/**
* db_mysql::rows()
*
* Returns the number of rows from the last query.
*
* @param mixed $rss
* @return number of rows
*/
function rows($rss)
{
$n = @mysql_num_rows($rss);
if($n === false)
{
$this->_seterror();
$n = 0;
}

return $n;
}

/**
* db_mysql::affected_rows()
*
* Returns the number of affected rows from the last query.
*
* @return number of rows
*/
function affected_rows()
{
$n = @mysql_affected_rows();
if($n === false)
{
$this->_seterror();
$n = 0;
}

return $n;
}

/**
* db_mysql::last_id()
*
* Returns the number of affected rows from the last query.
*
* @return last ID
*/
function last_id()
{
$n = @mysql_insert_id();
if($n === false)
{
$this->_seterror();
$n = 0;
}

return $n;
}

/**
* db_mysql::is_field()
*
* Returns true if the field exists in the given table. Ignores errors.
*
* @param mixed $tablename
* @param mixed $fieldname
* @return success
*/
function is_field($tablename, $fieldname)
{
$success = false;

$SQL = "SHOW COLUMNS
FROM $tablename";
if(!$rss = $this->query_read($SQL))
{
return false;
}
while($rs = $this->fetch_row($rss))
{
if($rs[0] == $fieldname)
{
$success = true;
break;
}
}
$this->free($rss);

return $success;
}
}

?>