[Sample Code] Simple Databse Management System
Here is a simple dbms that I made.
Save that as dbms.php.
Create a file called config.inc.php and put it in the site root.
Put the following code inside:
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:
Replace SELECT * FROM... with the query you want to perform. Check the dbms php file for more functions.
Use your new dbms wisely.
- Code: Select all
<?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:
- Code: Select all
<?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:
- Code: Select all
$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.