CarlLawl
09-13-2010, 10:02 PM
Ok so below is my version of a DB Class.
CODES:
require_once('cls/mysql.php'); // YOU CAN USE INCLUDE IF YOU WISH
$db = new Mysql();
$db->dbSelect('DBNAME'); // IF NOT SET IN THE CLASS FILE (no need for the connect func)
$db->connect(); // USE THIS IF ALL PARAMS ARE FILLED OUT IN THE CLASS FILE
$db->query('type in your mysql query'); // THIS IS FOR INSERT, UPDATE, DELETE
$db->fetch('type your SELECT mysql query here'); // THIS IS IF YOU ARE FETCHING ROWS
$myVar = $db->getResults(); // WILL PUT YOUR QUERY RESULTS IN A VAR AS ASSOC ARRAY (select queries only)
$myVarTwo = $db->getRows(); // WILL OUTPUT HOW MANY ROWS AS A NUMERIC FIGURE (select queries only)
$db->getPre(); // THIS IS ECHO OUT YOUR QUERY RESULTS FOR DEBUGGING/ERROR CHECKING
So heres the class file I've put together
<?php
class Mysql {
// SETS THE CONNECTION DETAILS
private $db_host = 'localhost';
private $db_user = 'DBUSER';
private $db_pass = 'DBPASS';
private $db_name = 'DBNAME';
// THIS IS WHERE THE QUERY RESULT WILL GO
private $result = array();
// THIS IS WHERE NUM ROWS IS STORED
private $numResults;
// CONNECTS TO DB
public function connect() {
$conn = mysql_connect($this->db_host,$this->db_user,$this->db_pass);
if ($conn) {
$db = mysql_select_db($this->db_name, $conn);
if ($db) {
$this->active = true;
return true;
}
else {
return false;
}
}
else {
return false;
}
}
// SELECTS A DB IF NOT SPECIFIED ABOVE
public function dbSelect($dbSelect) {
$this->db_name = $dbSelect;
$this->connect();
}
// GET VAR, ESCAPES THE STRING, EXEC QUERY, SETS RESULT
public function query($query) {
$query = mysql_escape_string($query);
$q = mysql_query("$query");
if ($q) {
return true;
}
else {
return false;
}
}
// FETCHES QUERY ROWS FROM DB
public function fetch($query) {
$query = mysql_escape_string($query);
$q = mysql_query("$query");
if ($q) {
$this->numResults = mysql_num_rows($q);
if ($this->numResults != 0) {
while ($f = mysql_fetch_assoc($q)) {
$this->result[] = $f;
}
}
else {
$this->result = null;
}
return true;
}
else {
return false;
}
}
// SENDS THE RESULT BACK
public function getResults() {
return $this->result;
}
// ECHOS OUT THE QUERY ARRAY RESULTS IN A READABLE FORMAT
public function getPre() {
echo '<pre>';
print_r($this->result);
echo '</pre>';
}
// SENDS THE NUM ROWS BACK
public function numResults() {
return $this->numResults;
}
}
?>
It also works in arrays for example.
if ($db->query) { } // IF YOUR QUERY IS INCORRECT OR FAILS IT WOULD RETURN BACK FALSE CARRYING OUT AN ELSE STATEMENT IF NEED BE
else { }
CODES:
require_once('cls/mysql.php'); // YOU CAN USE INCLUDE IF YOU WISH
$db = new Mysql();
$db->dbSelect('DBNAME'); // IF NOT SET IN THE CLASS FILE (no need for the connect func)
$db->connect(); // USE THIS IF ALL PARAMS ARE FILLED OUT IN THE CLASS FILE
$db->query('type in your mysql query'); // THIS IS FOR INSERT, UPDATE, DELETE
$db->fetch('type your SELECT mysql query here'); // THIS IS IF YOU ARE FETCHING ROWS
$myVar = $db->getResults(); // WILL PUT YOUR QUERY RESULTS IN A VAR AS ASSOC ARRAY (select queries only)
$myVarTwo = $db->getRows(); // WILL OUTPUT HOW MANY ROWS AS A NUMERIC FIGURE (select queries only)
$db->getPre(); // THIS IS ECHO OUT YOUR QUERY RESULTS FOR DEBUGGING/ERROR CHECKING
So heres the class file I've put together
<?php
class Mysql {
// SETS THE CONNECTION DETAILS
private $db_host = 'localhost';
private $db_user = 'DBUSER';
private $db_pass = 'DBPASS';
private $db_name = 'DBNAME';
// THIS IS WHERE THE QUERY RESULT WILL GO
private $result = array();
// THIS IS WHERE NUM ROWS IS STORED
private $numResults;
// CONNECTS TO DB
public function connect() {
$conn = mysql_connect($this->db_host,$this->db_user,$this->db_pass);
if ($conn) {
$db = mysql_select_db($this->db_name, $conn);
if ($db) {
$this->active = true;
return true;
}
else {
return false;
}
}
else {
return false;
}
}
// SELECTS A DB IF NOT SPECIFIED ABOVE
public function dbSelect($dbSelect) {
$this->db_name = $dbSelect;
$this->connect();
}
// GET VAR, ESCAPES THE STRING, EXEC QUERY, SETS RESULT
public function query($query) {
$query = mysql_escape_string($query);
$q = mysql_query("$query");
if ($q) {
return true;
}
else {
return false;
}
}
// FETCHES QUERY ROWS FROM DB
public function fetch($query) {
$query = mysql_escape_string($query);
$q = mysql_query("$query");
if ($q) {
$this->numResults = mysql_num_rows($q);
if ($this->numResults != 0) {
while ($f = mysql_fetch_assoc($q)) {
$this->result[] = $f;
}
}
else {
$this->result = null;
}
return true;
}
else {
return false;
}
}
// SENDS THE RESULT BACK
public function getResults() {
return $this->result;
}
// ECHOS OUT THE QUERY ARRAY RESULTS IN A READABLE FORMAT
public function getPre() {
echo '<pre>';
print_r($this->result);
echo '</pre>';
}
// SENDS THE NUM ROWS BACK
public function numResults() {
return $this->numResults;
}
}
?>
It also works in arrays for example.
if ($db->query) { } // IF YOUR QUERY IS INCORRECT OR FAILS IT WOULD RETURN BACK FALSE CARRYING OUT AN ELSE STATEMENT IF NEED BE
else { }