- Impact
- 89
Got a bit bored today (nothing on TV at Christmas huh) so i wrote a mysql class to share.
Enjoy, add to it...use it...do whatever you want.
Note this is untested, i don't see any problems but i haven't yet got around to trying it.
+Rep as always is appreciated if you use this. Consider it my x-mas present lol
Matt. :santa:
Enjoy, add to it...use it...do whatever you want.
Note this is untested, i don't see any problems but i haven't yet got around to trying it.
PHP:
<?php
/* ********** Mysql class **********
Written on 25th December 2006 (nothing on tv @ christmas)
Written by: Matt Jewell
http://3bigtrees.com
Feel free to modify and redistribute,
But please keep the written by lines intact.
Compatible with PHP: >=5.0.0
Additions/Modifcations:
[Name] [Details]
Matt First version written
*/
class mysql
{
protected $con; // Holds current connection info
private $n = 0; // Holds number of queries
//-----------------------------------------
# mysql_connect, mysql_select_db
//-----------------------------------------
public function __construct ($host = 'localhost', $un, $pw, $db)
{
$this -> con = @mysql_connect($host, $un, $pw);
if($this -> con)
{
$database = @mysql_select_db($db, $this -> con);
if(!$database)
{
trigger_error('Error ' . mysql_errno() . ' : ' . mysql_error(), E_USER_WARNING);
}
}
register_shutdown_function(array(&$this, "__destruct"));
}
//-----------------------------------------
# mysql query: mysql_query(sql, ident)
//-----------------------------------------
public function query ($sql)
{
$query = @mysql_query($sql, $this -> con);
if(!$query)
{
trigger_error('Error ' . mysql_errno() . ' : ' . mysql_error(), E_USER_WARNING);
}
else
{
$this -> n++;
return $query;
}
}
//-----------------------------------------
# mysql fetch array: mysql_fetch_array(query, ident)
# Type:
# MYSQL_ASSOC: default, returns as associative array.
# MYSQL_NUM: Return enumerated array
//-----------------------------------------
public function fetcharray ($query, $type = MYSQL_ASSOC)
{
return mysql_fetch_array($query, $type);
}
//-----------------------------------------
# mysql num rows() // use COUNT query instead to improve
//-----------------------------------------
function numrows($query)
{
return mysql_num_rows($query);
}
//-----------------------------------------
# Insert into database
# Use array for second argument
# e.g. arrray('fieldname' => 'value', 'field2' => 'value2')
# e.g. insert('blog', array('title' => 'hello', 'body' => 'content here'));
//-----------------------------------------
public function insert($table, $fields)
{
if(!sizeof($fields) > 0 || !is_array($fields))
{
trigger_error('Second argumenty for insert must be an array', E_USER_NOTICE);
}
else
{
foreach($fields as $key => $value)
{
$field_names[] = "`{$key}`";
$field_values[] = "'{$value}'";
}
$this -> query('INSERT INTO `' . $table . '` (' . implode(', ', $field_names) . ') VALUES (' . implode(', ', $field_values) . ')');
}
}
//-----------------------------------------
# return the number of queries used
//-----------------------------------------
public function countcon ()
{
return $this -> n;
}
//-----------------------------------------
# return mysql server information: mysql_get_seerver_info
//-----------------------------------------
public function serverinfo ()
{
return mysql_get_server_info($this -> con);
// return sprintf("%s", mysql_get_server_info($this -> con));
}
//-----------------------------------------
# Return a list of all tables in given database
# SHOW TABLES FROM database
//-----------------------------------------
public function list_tables ($database)
{
if(count($database) > 0)
{
return $this -> query('SHOW TABLES FROM `' . $database . '`');
// Example of use:
# while ($row = mysql_fetch_row($result, MYSQL_NUM))
# {
# echo "Table: {$row[0]}";
# }
//---------------------
}
}
//-----------------------------------------
# Mysql stats: mysql_stat(ident)
# Returns an enumerated array of connection statistics
# $return_array: Tells the function if it should return an array of results or not
# Standard result:
# Uptime: 357 Threads: 1 Questions: 140 Slow queries: 0 ...etc
# Put into an array:
# Array
# (
# [0] => Uptime: 5380
# [1] => Threads: 2
# [2] => Questions: 1321299
# [3] => Slow queries: 0
# ...etc
# )
//-----------------------------------------
public function mysql_stats ($return_array = false)
{
if($return_array == true)
{
return explode(' ', mysql_stat($this -> con));
}
else
{
return mysql_stat($this -> con);
}
}
//-----------------------------------------
# Disconnect from mysql: mysql_close(ident)
//-----------------------------------------
public function __destruct ()
{
mysql_close($this -> con);
}
}
// Create a new instance of the class
$db = new mysql('localhost', 'username', 'password', 'database');
?>
+Rep as always is appreciated if you use this. Consider it my x-mas present lol
Matt. :santa:
Last edited:





