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);
?>
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);
?>