I’ve just finished an small project for one company: it’s about “Registru de casa”, some paperwork that all romanian companies needs to mantain on daily basis. I’ve used PHP, Ajax-JQuery with MySQL database. Fast and clean. See here: www.tantar.info/registrucasa .
Here, i-ve build an new PHP Database class to help me:
1. First, how to use
$cDB=new tDatabase;
$cDB->dbConnect(DB_SERVER,DB_SERVER_USERNAME,DB_SERVER_PASSWORD,DB_DATABASE);
// now, to retrieve all tables from database:
$array_of_tables=$cDB->dbFetchTables();
echo("<pre>");print_r($array_of_tables);echo("</pre>");
<?php
/**
* @author Cristian Tantar <www.tantar.info> <contact@tantar.info>
* @copyright 2009
*/
class tDatabase extends tDebug {
var $connection=false;
function tDatabase() {
// do nothing
}
function dbConnect($host, $name, $pass, $db){
// conect to database
$connection = @mysql_connect("$host","$name", "$pass");
if(!$connection){
$this->PrintDebug(mysql_errno().":".mysql_error()."<BR> LINE ".__LINE__.", FILE: ".__FILE__,1);
die();
}
mysql_select_db("$db", $connection);
}//ends the connection function
function dbClose(){
// close database connection
mysql_close($this->connection);
}//ends the close function
function dbFetchRow($query){
// fetch one row with data from query
$rows = mysql_fetch_row($this->dbQuery($query));
return $rows;
}
function dbFetchArray($query){
// fetch array with data from query
$array = mysql_fetch_array($this->dbQuery($query));
return $array;
}
function dbFetchArrayResult($result){
// fetch array from resultset ( this result can be: $result=$db->query($sql) , or other)
$array = mysql_fetch_array($result);
return $array;
}
function dbFetchField($query,$field){
// return only specified field value, or n/a if empty
$row=$this->dbFetchArray($query);
if($row[$field]){
$field = $row[$field];
}else{
$field="n/a";
}
return $field;
}
function dbFetchFieldResult($result,$field){
// return specified field, but from result, instead of sql query
$row=$this->dbFetchArrayResult($result);
if($row[$field]){
$field = $row[$field];
}else{
$field="n/a";
}
return $field;
}
function dbFetchNum($query){
/* Get number of rows in query */
$num = mysql_num_rows($this->dbQuery($query));
return $num;
}
function dbFetchNumResult($result){
/* Get number of rows in result */
$num = mysql_num_rows($result);
return $num;
}
function dbFetchTables(){
// return array of tables in database
$result=mysql_list_tables(DB_DATABASE) or die (mysql_error());
$tables=array();
while($tbl=mysql_fetch_array($result)){
$tables[]=$tbl[0];
}
return $tables;
}
function dbLastId(){
// return last id generated from previous operation
return mysql_insert_id();
}
function dbFetchResultsQuery($sql){
// return array of results from query: $array[0]=row 0, $array[1]=row 1
// you can retreive data: $array[0][$field]
$resultArr=array();
$res=$this->dbQuery($sql);
while($data=$this->dbFetchArrayResult($res)){
$resultArr[]=$data;
}
return $resultArr;
}
function dbQuery($sql){
// return resultset of sql query
$resultSet=mysql_ query($sql) or die(mysql_error());
return $result;
}
}//ends the class
?>
<?php
/**
* simple debug class found on internet. not my property.
*/
class tDebug
{
var $logfile;
var $debug;
/*==============================================================================
Constructor
Arguments :- $debug_yesno. If 1 then errors will be logged,else displayed
==============================================================================*/
function hDebug($debug_yesno=0,$logfile_path="")
{
$this->debug=$debug_yesno;
$this->printdebug=1;
//Report only user generated error messages
error_reporting(0);
if($this->debug>=1)
{
$this->logfile=$logfile_path;
set_error_handler(array($this,"ErrorHandler"));
}
else
{
restore_error_handler();
}
}
/*==============================================================================
Following function logs an error message
==============================================================================*/
function ErrorHandler($errno,$errstr,$errfile,$errline)
{
$errtime=date("H:i:s - d/m/y");
$error_message="An error occured on ".$errtime."\n";
$error_message .="Details are as follows\n";
$error_message .="Error Number : $errno\n";
$error_message .=$errstr."\nOccured in ".$errfile."\n";
$error_message .="On Line $errline \n\n";
$error_message .=str_repeat("-",50);
$error_message .="\n";
$fp=fopen($this->logfile,"a+");
fwrite($fp,$error_message);
fclose($fp);
}
/*==============================================================================
Following function prints debug messages on the screen
==============================================================================*/
function PrintDebug($message,$yesno=0)
{
if($yesno>0)
{
$message="<font face=\"verdana\" size=\"2\" color=\"red\">Debug : ".$message."</font><br>";
print $message;
}
}
}
?>


Sorry, the comment form is closed at this time.