<?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
* # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* # 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'){
$this->host=$host;
$this->username=$username;
$this->password=$password;
$this->dbname=$dbname;
}
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)){
$this->connected=true;
return 1;
}else{
if($alert) echo 'Error while connecting db!';
$this->connected=false;
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;
if($this->connected)
return 'connected!';
else
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)){
$this->usingdb=$this->dbname;
return 1;
}else{
if($alert) echo 'Not Existing dbname!';
$this->usingdb=NULL;
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;
if($fetch){
$resultset=mysql_query($query);
$rows=array();
while($array = mysql_fetch_array($resultset)){
array_push($rows,$array);
}
return $rows;
}
return (int)mysql_query($query);
}
function getusingdb(){
// getusingdb() method echo the selected database
// echo $db->getusingdb() //<-echos the selected database
if($this->usingdb!=NULL)
return $this->dbname;
else
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,";
}
$query.=');';
$query=str_replace(',);',');',$query);
//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(";
$query2='';
foreach($values as $field => $val){
$query.="$field,";
$query2.="\"$val\",";
}
$query.=')';
//$query=str_replace(',)',')',$query);
$query.=' VALUES ('.$query2;
/*foreach($values as $field => $val)*/
$query.=');';
$query=str_replace(',)',')',$query);
//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()));
}
}
?>