Location: PHPKode > scripts > PHP MySQL databaser > class.Databaser.php
 * _______________________
 *|                       |
 *|  PHP-MySql Databaser  |
 *  		a very simple php class for using MySql database
 * 	copyleft by vikkio88 <hide@address.com>
 * *************************************
 * 	http://isnotnull.hellospace.net    *
 * *************************************
 *  #   This program is free software: you can redistribute it and/or modify
 *	#   it under the terms of the GNU General Public License as published by
 *	#   the Free Software Foundation, either version 3 of the License, or
 *	#   (at your option) any later version.
 *	#   This program is distributed in the hope that it will be useful,
 *	#   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	#   GNU General Public License for more details.
 *	#
 *	#   You should have received a copy of the GNU General Public License
 *	#   along with this program.  If not, see <http://www.gnu.org/licenses/>. 		
class Databaser{
	var $host;
	var $username;
	var $password;
	var $dbname;
	var $connected=false;
	var $usingdb=NULL;
	function __construct($dbname=NULL,$password=NULL,$username='root',$host='localhost'){
	function connect($alert=false){
		// connect() method try a connection to the mysqlserver
		  // $db=new Databaser();
		 // echo $db->connect(); //<-echos 0 if cannot conect you to the database
		if (@mysql_connect($this->host,$this->username,$this->password)){
			return 1;
			if($alert) echo 'Error while connecting db!';
			return 0;
	function isconnected($spoken=false){
		// isconnected() method control if you are connected to mysql server or not
		  // ..
		 // echo $db->isconnected();
		if(!($spoken)) return $this->connected;
			return 'connected!';
			return 'not connected!';
	function usedb($alert=false){
		// usedb() method try a connection to the selected database
		  // $db=new Databaser('Sample');
		 //  $db->connect(); 
		 //  echo $db->usedb(); //<-echoes 1 if database exist nd can be used
		if (@mysql_select_db($this->dbname)){
			return 1;
			if($alert) echo 'Not Existing dbname!';
			return 0;
	function simplequery($query=NULL,$fetch=true){
		// simplequery() method execute a query
		  // $rows=$db->simplequery('SELECT * FROM Books'); //<-return an array contains the rows of the Books table
		if($query==NULL) return NULL;
			while($array = mysql_fetch_array($resultset)){
			return $rows;
		return (int)mysql_query($query);
	function getusingdb(){
		// getusingdb() method echo the selected database
		  // echo $db->getusingdb() //<-echos the selected database
			return $this->dbname;
			return 'No db selected!';
	function createtable($tablename=NULL,$values=NULL){
		// createtable() method create a table in the selected database
		 // echo $db->$db->createtable('Example',array('ID'=>'INT PRIMARY KEY','name'=>'CHAR(25)'));
		 ////^echos 0 if cannot create the table
		if (($tablename == NULL) or ($values==NULL)) return -1;
		$query="CREATE TABLE $tablename (";
		foreach ($values as $field => $info){
			$query.="$field $info,";
		//echo $query;
		return (int)$this->simplequery($query,false);
	function droptable($tablename=NULL){
		//droptable() method simply delete a table from the selected db
		if ($tablename == NULL) return -1;
		$query="DROP TABLE $tablename;";
		return (int)$this->simplequery($query,false);
	function insert($tablename=NULL,$values=NULL){
		//droptable() method is very usefull for insert a record into the table
		//echo $db->insert('Example1',array('ID'=>2,'name'=>'JonnhyN'));
		// ^echos 0 if insert can be done
		if (($tablename == NULL) or ($values==NULL)) return -1;
		$query="INSERT INTO $tablename(";
		foreach($values as $field => $val){
		$query.=' VALUES ('.$query2;
		/*foreach($values as $field => $val)*/
		//echo $query;
		return (int)$this->simplequery($query,false);
	function tables(){
		//tables() method simply return an array that got name of all tables on db
		// $rows=$db->tables(); //<-return an array contains the rows of SHOW TABLES; query
			return $this->simplequery('SHOW TABLES;');
	function tablescount(){
		//tablescount() method is very usefull for count how many tables are there in the selected db
		return (int)(count($this->tables()));

Return current item: PHP MySQL databaser