PHP Classes

File: demo_classes_namespace_fhDatabaseInterface.php

Recommend this page to a friend!
  Packages of Frank Hartung   Database Interface   demo_classes_namespace_fhDatabaseInterface.php   Download  
File: demo_classes_namespace_fhDatabaseInterface.php
Role: Example script
Content type: text/plain
Description: Example-Script for the classes
Class: Database Interface
Access MySQL databases using the MySQLi extension
Author: By
Last change:
Date: 12 years ago
Size: 8,958 bytes
 

 

Contents

Class file image Download
<?php
// file: demo_classes_namespace_fhDatabaseInterface.php - Example File, version 20140304 2322
// fhDatabaseInterface; classes and functions by frank hartung
// created in php/5.5.9 for mysqlnd 5.0.11-dev - 20120503
// successfully tested with php/5.3.1 for mysqlnd 5.1.41
// 
echo 'file: demo_classes_namespace_fhDatabaseInterface.php - Demo/Example File'.'<br><br>';

require_once('exec_classes_namespace_fhDatabaseInterface.php');
use  fhDatabaseInterface\DatabaseConnection
    ,fhDatabaseInterface\DatabaseQuery
    ,fhDatabaseInterface\DatabaseFunc
;

//examples of usage..
echo'<br>$_objDbCon :';
echo'<br>';var_dump($_objDbCon);
echo'<br>$_IdDbCon :';
echo'<br>';var_dump($_IdDbCon);
echo'<br>';

echo'<br>prepare to getlist for table config..';
$result=fhDatabaseInterface\DatabaseFunc::getList('config');
echo'<br>';var_dump($result);

global $_config;
$_config=fhDatabaseInterface\DatabaseFunc::getDbConfig();//load config from database.table ..config into array $_config
echo'<br>$_config :';
echo'<br>';var_dump($_config);

$result=fhDatabaseInterface\DatabaseFunc::valueInArray( fhDatabaseInterface\DatabaseFunc::execute('select database();') );
echo'<br>'.$result;

//since use-settings are executed before like on top of this file(!) with lines:
//         use  fhDatabaseInterface\DatabaseConnection
//             ,fhDatabaseInterface\DatabaseQuery
//             ,fhDatabaseInterface\DatabaseFunc
//         ;
//.. you can miss to give the name of the namespace..
$result=DatabaseFunc::request('select database();');
echo'<br>current/selected database: '.$result;

$now=DatabaseFunc::request('select now();');
echo'<br>'.$now;

$result=DatabaseFunc::request("SHOW DATABASES LIKE '"._sql_dbName."';", NULL, 'array' );
echo'<br>result of database show: '.$result;var_dump($result);
if($result==_sql_dbName)
    echo'<br>database '._sql_dbName.' does already exists!';
if($result!=_sql_dbName || $result==NULL)
{//database does not exists..
    $result=DatabaseFunc::request("CREATE DATABASE '"._sql_dbName."';");
    if($result==false)
    echo'<br>create database failed!';
    echo'<br>result of database creation: '.$result;var_dump($result);
}

$result=DatabaseFunc::request("SHOW TABLE STATUS LIKE '"._sql_dbTblPrefix."config';");
echo'<br>result of database table status show: '.$result;var_dump($result);
if($result!=_sql_dbTblPrefix.'config' || $result==NULL)
{//database does not exists..
    $result=DatabaseFunc::request("CREATE TABLE IF NOT EXISTS `"._sql_dbTblPrefix."config` (
      `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
      `name` varchar(30) NOT NULL,
      `string` varchar(50) NOT NULL,
      `flag` bigint(20) NOT NULL DEFAULT '0' COMMENT 'byte / numeric ..flag',
      `origin` varchar(80) NOT NULL COMMENT 'origin of last modification at time in time',
      `time` int(11) NOT NULL DEFAULT '0',
      `info` varchar(80) NOT NULL,
      `text` text NOT NULL,
      `update` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
      PRIMARY KEY (`id`),
      UNIQUE KEY `name` (`name`)
    ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 COMMENT='configuration ram replacement' AUTO_INCREMENT=2;");
    echo'<br>';var_dump($result);
}

//please notice that get-data(sql select) commands like getList/getRow/getField-functions including automatically table-prefix usage/extend..so you just only have to give the shorted targetname of table like config for myprefix_config:
$row=DatabaseFunc::getRow('config', "`name`='masterswitch'", '*', '`id` ASC', '0', NULL, NULL, NULL, true);//9th param is debug with value true!
if($row['name']!='')
    echo'<br>db-table-entry exists - no need for insert entry!';
else
{
    echo'<br>db-table-entry does not exists - insert new db-table-extry..';
$result=DatabaseFunc::request("INSERT INTO `"._sql_dbTblPrefix."config` (`id`, `name`, `string`, `flag`, `origin`, `time`, `info`, `text`, `update`) VALUES (NULL, 'masterswitch', '', 1, '', 0, '1:enable 0:disable site', '', '2014-02-24 12:22:04');");
if($result==false)
    echo'<br>insert failed!';
echo'<br>';var_dump($result);
}

echo'<br>lookup to one field of an single row..';
$field=DatabaseFunc::getField('config', "`name`='masterswitch'", "`flag`", '`id` ASC', '0', NULL, NULL, NULL, true);//9th param is debug with value true!
if($field!='1')
    echo'WARNING: config masterswitch is disabled due to the flag value: '.$field;
else
    echo'info: config masterswitch is enabled due to the flag value: '.$field;
//other style of insert-example.. by the keys/values princip
$result=DatabaseFunc::insert('config','`name`,`flag`',"'whatever', 1" );
if($result==false)
    echo'<br>insert failed!';
echo'<br>';var_dump($result);

//another example of insert in simplified form..
$result=DatabaseFunc::insertString('config', "`name`='whatever2', `flag`=2" );
if($result==true)
    echo'<br>insert was successful.';
echo'<br>';var_dump($result);


$result=DatabaseFunc::update('config', "`name`='whatever2'", "`flag`=3" );
if($result==true)
    echo'<br>update was successful.';
echo'<br>';var_dump($result);

$field=DatabaseFunc::getField('config', "`name`='whatever2'", "`flag`", '`id` ASC', '0', NULL, NULL, NULL, true);//9th param is debug with value true!
if($field=='3')
    echo'verify of update confirmed!';
else
    echo'verify of update NOT confirmed!(=FAIL!)';

    
$result=DatabaseFunc::replace('config', "`name`, `flag`", "'whatever2', 4" );
if($result==true)
    echo'<br>replace(update-variant) was successful.';
echo'<br>';var_dump($result);

$field=DatabaseFunc::getField('config', "`name`='whatever2'", "`flag`", '`id` ASC', '0', NULL, NULL, NULL, true);//9th param is debug with value true!
if($field=='4')
    echo'verify of replace(update) confirmed!';
else
    echo'verify of replace(update) NOT confirmed!(=FAIL!)';


$list=DatabaseFunc::getList('config');//array of all entries..all fields..order asc
echo'<br>'.count($list).' entries!';
foreach($list AS $key=>$data)
{
    echo'<br>'.$key.'='.$data.' contains..';
    echo'<br>name '.$data['name'].' in database row with id '.$data['id'];
}

$list=DatabaseFunc::getList('config', "name like '%'", 'id,name', 'id DESC');//array of all entries..this time in DESC order..but only field id and name
echo'<br>'.count($list).' entries! ..in order of DESC';
foreach($list AS $key=>$data)
{
    echo'<br>'.$key.'='.$data.' contains..';
    echo'<br>name '.$data['name'].' in database row with id '.$data['id'];
}

//now lets explore the details of the fields...
$list=DatabaseFunc::getList('config', "1", '*', 'name ASC');//table config; array of all(1) entries..this time in ASC order by field name..and all('*') fields
echo'<br>'.count($list).' entries! ..in order of DESC';
foreach($list AS $key=>$line)
{
    echo'<br>'.$key.'='.$data.' contains..';
    foreach($line AS $field=>$value)
    {
        echo'<br> field/value ';
        if($field=='name')
            echo'<b>';
        if($field=='name' && $value=='masterswitch')
            echo'<u>';
        echo $field.' = '.$value.'</b></u>';
    }
}

//exploring an table with their fieldnames.. for reason (example) of creating an table simple header for giving out an list..
$result=DatabaseFunc::getTableFields('config', NULL, true);
echo'<br>fieldnames of table ..'.count($list).' entries! ..in order of DESC';
if(count($result)>0)
{//view the header due to the tablefields-list
    echo'<table><tr>';//open table-display with first row
    foreach($result AS $key=>$line)
    {
        foreach($line AS $title=>$value)
        {
            if($title=='Field')
                $name=$value;
            elseif($title=='Type')
                $type=$value;
        }
        echo'<td><b>'.$name.'</b> <small>('.$type.')</small></td>';//fill row with data
    }
    echo'</tr><br>';
    //now display the content of table-rows in the html table..
    $list=DatabaseFunc::getList('config', "1", '*', 'id ASC');//table config; array of all(1)         
    if(count($list)>0)
    {
        foreach($list AS $key=>$line)
        {
            echo'<tr>';
            foreach($line AS $field=>$value)
            {
                echo'<td>'.$value.'</td>';
            }
            echo'</tr>';
        }
            
    }
    echo'</table><br>';//close table-display
}
else
    echo'<br>field-result are empty!';

$result=DatabaseFunc::getDatabaseCreate(NULL, NULL, true);//show default database create with debug true
echo'<br>show result of Database-Create:<br>';
var_dump($result);
$query=$result[0]['Create Database'];
echo'<br>sql-query to create Database: <br>'.$query.'<br>';

$result=DatabaseFunc::getTableCreate('config', NULL, true);//show table 'config' create with debug true
echo'<br>show result of Table-Create of table ,,config,,:<br>';
var_dump($result);
$query=$result[0]['Create Table'];
echo'<br>sql-query to create Table: <br>'.$query.'<br>';

$query2=DatabaseFunc::getQueryTableCreate('config', NULL, true);//show table 'config' create with debug true
if($query===$query2)
    echo'<br>create of table query result is verified to be exact!!';
else
    echo'<br>create table query result is NOT confirmed to be verified!';

//eof