This class is a basic Mysql database access wrapper. It can:
-Establish an association with a given Mysql server host
-Execute Sql questions
-Retrieve question comes about into exhibits
-Retrieve the outcome set number of columns, final embedded identifier or the amount of influenced columns
-Check if a database table record exists matching a given condition
-Retrieve the amount of records that a table has matching a given condition
Below is class:
class.php:
<?php
class WGDB{
public $connection ;
public $querycount = 0;
public function db() {}
public function connect($dbhost, $dbusername, $dbpassword)
{
$this->connection = mysql_connect($dbhost,$dbusername,$dbpassword);
return $this->connection;
}
public function close()
{
return mysql_close( $this->connection );
}
public function select_db($databasename)
{
return mysql_select_db($databasename,$this->connection);
}
public function exist($tablename, $fieldname, $value, $where = false)
{
$exist = $this->fastfetch(“select * from `$tablename` where `$fieldname` = ‘$value’ “.check($where, ” $where”).””);
return check($exist[‘id’], true, false);
}
public function count($tablename, $where = false)
{
$query = $this->query(“select * from `$tablename` “.check($where, “where $where”).””);
return $this->fetchnum($query);
}
public function query($sql)
{
$this->querycount++;
return mysql_query($sql,$this->connection);
}
public function fastfetch($sqlcode, $type = MYSQL_BOTH )
{
$query = $this->query($sqlcode) ;
return $this->fetcharray($query, $type);
}
public function fetcharray($query, $type = MYSQL_BOTH )
{
return mysql_fetch_array($query, $type);
}
public function fetchassoc($query)
{
return mysql_fetch_assoc($query);
}
public function fetchnum($query)
{
return mysql_num_rows($query);
}
public function fetchobject($query)
{
return mysql_fetch_object($query);
}
public function escape($string)
{
return mysql_escape_string($string);
}
public function insertid()
{
return mysql_insert_id($this->connection);
}
public function result()
{
return mysql_affected_rows($this->connection);
}
}
?>
example.php
<?php
include('class.php');
$dbhost = ‘localhost’;
$dbusername = ‘root’;
$dbpassword = ”;
$databasename = ‘test’;
$db=new WGDB();
if(!$db->connect($dbhost,$dbusername,$dbpassword)) die(‘conn. error’.mysql_error());
if(!$db->select_db($databasename)) die(‘db. error’.mysql_error());
?>