Somebody asked to me to write a simple mysql manager for simple database application.
I have created a simple script here.
<?php
/*
##================================================ =========================================
## Mysql Manager Version 1.0 => Beta Release.
## Mysql Utilities for Web Based application .
## Written By Kiran Krishnan =>
smart_coder@yahoo.co.in
## Copyright to smartcoderin ->smartcoderin@yahoo.co.in
## Platform Mysql 5.0 or above with PHP 5.0 or above.
## Phase : Development : Started on 31-January - 2008 : Test remain
## Tested on Wamp Server with Mysql 4.2
##================================================ ===========================================
*/
class MysqlManager
{
/*
Global varibaless accessible outside the utility
Rendered for easy access of functionality
*/
var $QueryString; //exec query String //
var $QueryResult; //Resultnat ARray
var $QueryRows; //Num rows
var $QueryError; //QueryErrors //
var $DbUser;
var $DbPass;
var $DbHost;
var $DbCon;
var $DbName;
var $field_count;
var $row_count;
/*
*/
function MysqlManager()
{
//Initializing Mysql Manager //
$this->QueryString = '';
$this->QueryResult = '';
$this->QueryRows = '';
$this->QueryError = '';
$this->DbUser = "root"; //server auth user //
$this->DbPass = ""; //server auth pass //
$this->DbHost = "localhost"; //server address //
$this->DbName = "folklore"; //
$this->field_count = 0;
$this->row_count = 0;
}
//Nothing just displays the error //
function _mysql_error($error)
{
$this->QueryError = $error;
trigger_error("Mysql Manager Said : ".$this->QueryError,E_USER_NOTICE); //call error trigger //
}
//close mysql connection //
function _close_mysql()
{
if(!$this->DbCon)
{
mysql_close($this->DbCon); //closing the mysql server //
}
}
//connect to mysql database //
function _connect_mysql()
{
$this->DbCon = @mysql_connect($this->DbHost,$this->DbUser,$this->DbPass); //Conect Myql Server //
if(!$this->DbCon)
{
$this->_mysql_error(mysql_error());
}
else
{
$this->_select_database();
}
}
//Now select database for the application//
function _select_database()
{
if(empty($this->DbName))
{
$this->_mysql_error("Database Name Empty ");
}
else
{
$db = @mysql_select_db($this->DbName);
if(!$db)
{
$this->_mysql_error("Cannot Find the Database ");
}
}
}
//execute Query section //
function _exec_query($query)
{
$this->QueryString = $query;
$this->QueryResult = @mysql_query($this->QueryString);
if(!$this->QueryResult)
{
//
$this->_mysql_error(mysql_error());
return false;
}
else
{
return true;
}
}
//Get all fields from the table //
function _table_describe($table_name)
{
$this->QueryString = "DESCRIBE `".$table_name."` ";
if($this->_exec_query($this->QueryString))
{
if(mysql_num_rows($this->QueryResult)>0)
{
while($row = mysql_fetch_row($this->QueryResult))
{
$field_name = $row[0];
}
}
}
}
//execute Simple query //
function _simple_query($query,$type)
{
$this->QueryString = $query;
if($this->_exec_query($this->QueryString))
{
if(strtolower($type)=="select")
{
$this->_format_result(); //format the results only for select statement //
}
}
}
//--->format the result set after selection<---- //
function _format_result()
{
$num_fields = mysql_num_fields($this->QueryResult);
if(mysql_num_rows($this->QueryResult)>0)
{
$arr_cnt=0;
while($row=mysql_fetch_row($this->QueryResult))
{
for($i=0;$i<$num_fields;$i++)
{
$field_name = mysql_field_name($this->QueryResult,$i);
$this->row_values[$arr_cnt][$field_name] = remove_slashes($row[$i]);
$this->row_name[$i] = $field_name;
}
$arr_cnt++;
}
}
else
{
$arr_cnt = 0;
}
$this->field_count = $num_fields; //Total fields selected //
$this->row_count = $arr_cnt; //total records found //
}
function _clear_mysql()
{
@mysql_free_result($this->QueryResult);
$this->QueryString="";
unset($this->field_count);
unset($this->row_count);
unset($this->row_name);
unset($this->row_values);
}
}
?>
To download code snippets you can visit
SmartCoderIn => Scripts and tutorials : We give online assistance