| px | top | add code | search | signup | login | help |
<?php
/* Database Class v1.0
*
* by Jonathan Meyer
* Feb 2002
*
* Latest version available here:
* http://px.sklar.com/code.html?id=902
*
*/
/* Table.php is available from here:
* http://px.sklar.com/code.html?id=903
*/
require_once( 'Table.php' );
class Database
{
var $server;
var $database;
var $username;
var $password;
var $link;
var $delete_field;
function Database( $server, $database, $user, $pass )
{
$this->server = $server;
$this->database = $database;
$this->username = $user;
$this->password = $pass;
$this->link = false;
$this->delete_field = '';
}
function set_del( $field )
{
$this->delete_field = $field;
}
function set_delete_field( $field )
{
$this->delete_field = $field;
}
function connect()
{
if( ! $this->link )
{
$this->link = mysql_connect( $this->server, $this->username, $this->password );
}
return $this->link;
}
function hash_2_string( $hash )
{
if( func_num_args() == 2 )
{
$delim = func_get_arg(1);
}
else
{
$delim = ',';
}
$str = '';
foreach( $hash as $k => $v )
{
if( $str != '')
{
$str = "$str$delim ";
}
if( is_int( $v ) )
{
$str = "$str`$k` = $v";
}
else
{
$str = "$str`$k` = '$v'";
}
}
return $str;
}
function get_table( $table )
{
$sql = "SELECT * FROM `$table`";
$t = $this->run_query( $sql );
return $t;
}
// get_sorted_table( table_name, <- table to get
// field_name, <- field to sort by
// [ up | down ], <- sort up or down
// [ field_name ], <- only give records with this field...
// [ value ] ) <- equaling this value
//
function get_sorted_table( $table, $sort='id', $order='up', $field='', $value='' )
{
$order == 'down' ? $direction = 'DESC' : $direction = 'ASC';
$sort == '' ? $sort = 'id' : 1;
if( $field and $value )
$sql = "SELECT * FROM `$table` WHERE `$field` = '$value' ORDER BY `$sort` $direction";
else
$sql = "SELECT * FROM `$table` ORDER BY `$sort` $direction";
return $this->run_query( $sql );
}
function get_record( $table, $hash )
{
$where = $this->hash_2_string( $hash, ' AND' );
$sql = "SELECT * FROM `$table` WHERE $where";
$t = $this->run_query( $sql );
return $t;
}
function set_record( $table, $field, $value, $record )
{
$sql = "UPDATE $table SET ".$this->hash_2_string($record)." WHERE `$field` = '$value'";
$t = $this->run_query( $sql );
return true;
}
function set_record_where( $table, $hash, $record )
{
$sql = "UPDATE $table SET ".$this->hash_2_string($record)." WHERE ".$this->hash_2_string($hash,'AND');
$t = $this->run_query( $sql );
return true;
}
function del_record( $table, $field, $value )
{
$r = $this->get_record( $table, array( $field => $value ) );
$r->records[0][$this->delete_field] = 'Y';
return $this->set_record( $table, $field, $value, $r->records[0] );
}
function undel_record( $table, $field, $value )
{
$r = $this->get_record( $table, array( $field => $value ) );
$r->records[0][$this->delete_field] = 'N';
return $this->set_record( $table, $field, $value, $r->records[0] );
}
function add_record( $table, $record )
{
$sql = "INSERT $table SET ".$this->hash_2_string($record);
$this->run_query( $sql );
return mysql_insert_id( $this->link );
}
function get_user( $table, $email, $pass )
{
$sql = "SELECT id,name,email,admin_priv FROM `$table` WHERE `email` = '$email' AND `pass` = PASSWORD('$pass')";
$t = $this->run_query( $sql );
if( $t->count() == 1 )
{
return $t->row(0);
}
return false;
}
function set_pass( $table, $id, $newpass )
{
$sql = "UPDATE $table SET `pass` = PASSWORD('$newpass') WHERE `id` = '$id'";
$t = $this->run_query( $sql );
return true;
}
function run_query( $sql )
{
global $S;
if( ! $this->link )
{
$this->connect();
if( ! isset( $S['count'] ) )
$S['count'] = 0;
$S['count']++;
}
if( $this->link )
{
$t = new Table();
mysql_select_db($this->database, $this->link);
$result = mysql_query( $sql, $this->link );
if( $result )
{
if( preg_match( "/SELECT|SHOW/i", $sql ) )
{
$rows = mysql_num_rows( $result );
for( $r = 0; $r < $rows; $r++ )
{
$re = mysql_fetch_assoc( $result );
if( ! ($this->delete_field and $re[$this->delete_field] == 'Y') )
{
$t->add_record( $re );
}
}
}
}
else
{
print "*** Error ".mysql_error()." ***";
}
}
else
{
$t = false;
}
return $t;
}
function get_field_type( $table, $field )
{
$sql = "SHOW FIELDS FROM $table";
$t = $this->run_query( $sql );
$type = false;
foreach( $t->records as $r )
{
if( $r['Field'] == $field )
{
$type = $r['Type'];
}
}
return $type;
}
function get_enum_list( $table, $field )
{
$type = $this->get_field_type( $table, $field );
$list = array();
if( preg_match( "/^enum/i", $type ) )
{
foreach( preg_split( "/,/", preg_replace( "/enum(([^)]+))/", "\1", $type ) ) as $e )
{
array_push( $list, preg_replace( "/'([^']+)'/", "\1", $e) );
}
}
else
{
array_push( $list, $field.' is not an enum type' );
}
return $list;
}
}
php?>
Comments or questions?
PX is running PHP 5.2.11
Thanks to Miranda Productions for hosting and bandwidth.
Use of any code from PX is at your own risk.