PDA

View Full Version : [RELEASE] Simple PHP database core.



Dentafrice,
26-10-2007, 01:47 AM
I made this earlier today, I am going to begin using it in most of my applications:

Here is the class:

<?php
/**
* @author Caleb Mingle
* @copyright 2007
* @email [email protected]
* http://www.calebmingle.com
* Project: NONE.
* File: DB core.
*/

class db
{
var $server = "localhost";
var $username;
var $password;
var $database;
var $persistant;


function connect()
{
if ($this->persistant == "yes")
{
$link = @mysql_pconnect($this->server, $this->username, $this->password);
}
else
{
$link = @mysql_connect($this->server, $this->username, $this->password);
}

if ($link == false)
{
echo "Cannot connect to MySQL server.";
if ($this->persistant == "yes")
{
echo "<br><strong>Using persistant connection:</strong> YES<br>";
}
echo "<br><br><strong>Received from MySQL server:</strong><br>";
echo mysql_error();
exit;
}
$this->select_db();
}

function select_db()
{
$db = @mysql_select_db($this->database);
if ($db == false)
{
echo "Cannot select DB: <strong>$this->database</strong>.";
if ($this->persistant == "yes")
{
echo "<br><strong>Using persistant connection:</strong> YES<br>";
}
echo "<br><br><strong>Received from MySQL server:</strong><br>";
echo mysql_error();
exit;
}
}

function query($query)
{
$r_query = @mysql_query($query);
if ($r_query == false)
{
echo "Cannot complete query: $query";
if ($this->persistant == "yes")
{
echo "<br><strong>Using persistant connection:</strong> YES<br>";
}
echo "<br><br><strong>Received from MySQL server:</strong><br>";
echo mysql_error();
exit;
}
return $r_query;
}

function fetch_array($resource)
{
$r_array = @mysql_fetch_array($resource);
if ($r_array == false)
{
echo "Cannot complete fetch_array for this resource.";
if ($this->persistant == "yes")
{
echo "<br><strong>Using persistant connection:</strong> YES<br>";
}
echo "<br><br><strong>Received from MySQL server:</strong><br>";
echo mysql_error();
exit;
}
return $r_array;
}

function num_rows($resource)
{
$r_num = @mysql_num_rows($resource);
if ($r_num == false)
{
echo "Cannot complete num_rows for this resource.";
if ($this->persistant == "yes")
{
echo "<br><strong>Using persistant connection:</strong> YES<br>";
}
echo "<br><br><strong>Received from MySQL server:</strong><br>";
echo mysql_error();
exit;
}
return $r_num;
}

function clean($string)
{

$string = htmlspecialchars($string, ENT_QUOTES);

if (get_magic_quotes_gpc())
{

$string = stripslashes($string);

}

$string = str_replace("\"", "", $string);
$string = htmlentities($string);
$string = mysql_real_escape_string($string);

return $string;

}


}

?>

You can save that where you want it, just be sure to edit the location in your config file:


<?php
/**
* @author Caleb Mingle
* @copyright 2007
* @email [email protected]
* http://www.calebmingle.com
* Project: NONE.
* File: Main configuration.
*/

// Main includes config //

define("MAIN_ROOT", "/home/username/public_html/project/"); // Location of where your project is.
define("CLASSES_ROOT", MAIN_ROOT . "classes/"); // Location of classes, if classes are in same directory as config leave as ""

// Include //
include (CLASSES_ROOT . "db_core.php");

// MySQL information //
$db = new db;
$db->server = "localhost"; // MySQL server, leave as localhost unless directed otherwise.
$db->username = ""; // MySQL username.
$db->password = ""; // MySQL password.
$db->database = ""; // MySQL database.

// Optional information //
$db->persistant = "no"; // MySQL persistant connection.

// MySQL connect //
$db->connect();

?>

To use:

<?php

// Query //

$db->query("INSERT INTO bla (test) VALUES('hey!')");

// Num rows //

$query = $db->query("SELECT * FROM bla WHERE id='1'");
$num = $db->num_rows($query);

// Fetch Array //

$query = $db->query("SELECT * FROM bla WHERE id='1'");
$rows = $db->fetch_array($query);

// Clean //

$string = "test";
$string = $db->clean($string);

?>

VistaBoy
26-10-2007, 06:16 AM
looks nice. how long did it tack to code it up?

Tomm
26-10-2007, 06:57 AM
Guess where you got the idea from and the basic code :P

Jamie.
26-10-2007, 07:40 AM
Looks good :)! credit goes to you caleb.

Dentafrice,
26-10-2007, 02:37 PM
Guess where you got the idea from and the basic code :P
I got the IDEA from you :P But coded it myself.

Invent
26-10-2007, 04:51 PM
Guess where you got the idea from

I don't get how he got the idea from someone else :S It's a very simple class, lol.

Dentafrice,
26-10-2007, 06:19 PM
Na, I had never thought about using the fetch_array and num_rows until Tom showed me his :)

Invent
26-10-2007, 09:24 PM
Na, I had never thought about using the fetch_array and num_rows until Tom showed me his

Ah, ok I getcha.

I thought he meant you got the idea of a DB Class from him :P

Dentafrice,
26-10-2007, 11:16 PM
Nope :P

Want to hide these adverts? Register an account for free!