Articles: 843 | Categories: 148   
   
   
Home Articles Contact Us
 
 
 
 
Simple DB Management System (0 Comments)
Admin: Posted Date: March 3, 2010

In this tutorial, you will the steps that ar needed to not only learn but create a simple but effective database management system.

Simple DB Management System

Here is a simple dbms that I made.

<?php
class db
{
    function 
connect()
    {
        include(
$_SERVER['DOCUMENT_ROOT'] . '/config.inc.php');
        switch(
$type)
        {
            case 
"mysql":
                
mysql_connect($host$user$pass) or die("Could not connect: " mysql_error());
                
mysql_select_db($name) or die(mysql_error());
                break;
            case 
"mssql":                mssql_connect($host$user$pass) or die("Could not connect!");
                
mssql_select_db($name);
                break;
            case 
"oracle":
                
$_SESSION['con'] = oci_connect($host$user$pass$name);
                break;
        }
    }
    function 
query($q)
    {
        include(
$_SERVER['DOCUMENT_ROOT'] . '/config.inc.php');
        switch(
$type)
        {
            case 
"mysql":
                return 
mysql_query($q);
                break;
            case 
"mssql":
                return 
mssql_query($q);
                break;
            case 
"oracle":
                return 
oci_parse($_SESSION['con'], $q);
                break;
        }
    }
    function 
numrows($q)
    {
        include(
$_SERVER['DOCUMENT_ROOT'] . '/config.inc.php');
        switch(
$type)
        {
            case 
"mysql":
                return 
mysql_num_rows($q);
                break;
            case 
"mssql":
                return 
mssql_num_rows($q);
                break;
            case 
"oracle":
                return 
oci_num_rows($q);
                break;
        }
    }
    function 
fetcharray($q)
    {
        include(
$_SERVER['DOCUMENT_ROOT'] . '/config.inc.php');
        switch(
$type)
        {
            case 
"mysql":
                return 
mysql_fetch_array($q);
                break;
            case 
"mssql":
                return 
mssql_fetch_array($q);
                break;
            case 
"oracle":
                return 
oci_fetch_array($q);
                break;
        }
    }
    
/* Use this before inserting into database */
    
function sanitize($dirty)
    {
        
$dirty trim($dirty);
        
$dirty htmlspecialchars($dirty);
        include(
$_SERVER['DOCUMENT_ROOT'] . '/config.inc.php');
        switch(
$type)
        {
            case 
"mysql":
                
$dirty mysql_real_escape_string($dirty);
                break;
            case 
"mssql":
                
$dirty mysql_real_escape_string($dirty);
                break;
            case 
"oracle":
                
$dirty mysql_real_escape_string($dirty);
                break;
        }
        return 
$dirty;
    }
    
/* Use this before displaying database data */
    
function scrub($dirty)
    {
        
$dirty stripslashes($dirty);
        return 
$dirty;
    }
}
?>

Save that as dbms.php.

Create a file called config.inc.php and put it in the site root.

Put the following code inside:

<?php
/* Config File  */
$host="your_host";
$name="your_db"
$user="your_user";
$pass="your_pass";
$pfix="your_db_prefix";
$type="db_type";
?>
Replace your_host with your database host.
Replace your_db with your database name.
Replace your_user with your database username.
Replace your_pass with your database password.
Replace your_db_prefix with the table prefix.
Replace db_type with either mysql, mssql, or oracle.

In the the files you want to access the database. Include the dbms file. Then add the following code:
$db &= new db;
$db->connect();
$db->query("SELECT * FROM...");   

Replace SELECT * FROM... with the query you want to perform. Check the dbms php file for more functions.

Use your new dbms wisely.

 

 
 
Add a Comment:
 
(You must be signed in to comment on an article. Not a member? Click here to register)
   
Title:

Comments: