Results 1 to 10 of 15
Threaded View
-
23rd Jul 2010, 04:25 AM #1OPMemberWebsite's:
Doxsters.netDatabase Class
Hey guys,
I'm learning OOP PHP and I created a basic class to help out with database handling.
Here it is, fell free to use and modify it. If you do modify it, it'd be cool if you post here as it would be interesting to see what people have done with it
Anyway:
PHP Code://Basic Class to deal with databases, created by timtamboy63
class Database {
protected $dbuser = 'root';
protected $dbpass = 'passwordhere';
protected $dbhost = 'localhost';
protected $dbname;
public $connection;
public $queryresult;
public $query;
public $numrows;
function Database($dbname){
//the constructor function
$this->dbname = $dbname;
}
function connect(){
$this->connection = mysql_connect($this->dbhost, $this->dbuser, $this->dbpass) or die(mysql_error());
mysql_select_db($this->dbname, $this->connection) or die('Database could not be selected, Error: ' . mysql_error());
return true;
}
function disconnect(){
mysql_close($this->connection) or die('Could not close database. Error: ' . mysql_error());
return true;
}
function query($query, $type = 2){
//type can be either 0 which does not do anyhting to it
//it can be 1 which will just sanitize the mysql query with mysql_real_escape_string
//or it can be 2 which will also apply htmlentities(and protect against xss), $this is reccomended for anything that gets stored in the database and displayed later
switch($type){
case 0:
$this->query = trim($query);
$this->queryresult = mysql_query($this->query);
return true;
break;
case 1:
$this->query = mysql_real_escape_string(trim($query));
$this->queryresult = mysql_query($this->query);
return true;
break;
case 2:
$this->query = htmlentities(mysql_real_escape_string(trim($query)));
$this->queryresult = mysql_query($this->query);
return true;
break;
default:
die('Function query in class database used incorrectly. The second argument only accepts the values 0, 1 and 2(default & most secure). Read the source for more information');
return false;
break;
}
}
function fetchArray(){
while($row = mysql_fetch_array($this->queryresult)){
//change this to whatever you want.
print $row['id'] . '
';
}
}
function numRows(){
$this->numrows = mysql_num_rows($this->queryresult);
return $this->numrows;
}
}
/* Example usage:
$phonebook = new Database('Phonebook');
$phonebook->connect();
$query = 'SELECT * FROM phonebook';
$phonebook->query($query);
$phonebook->fetchArray();
$phonebook->disconnect();
*/
?>
timtamboy63 Reviewed by timtamboy63 on . Database Class Hey guys, I'm learning OOP PHP and I created a basic class to help out with database handling. Here it is, fell free to use and modify it. If you do modify it, it'd be cool if you post here as it would be interesting to see what people have done with it http://chrixel.com/forums/public/style_emoticons/default/tongue.gif Anyway: //Basic Class to deal with databases, created by timtamboy63 class Database { protected $dbuser = 'root'; Rating: 5
Need a Designer/Web Developer? Click Me
MSN: PM me for it.
Email(Preferred):timtamboy63@gmail.com
"Power Corrupts. Absolute Power Corrupts Absolutely"
Sponsored Links
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Similar Threads
-
NoFollow Class?
By DJSquared in forum Web Development AreaReplies: 6Last Post: 27th Nov 2011, 05:35 AM -
I need many class C IP's
By filat in forum Hosting DiscussionReplies: 12Last Post: 17th Oct 2011, 08:54 PM -
Should I have my class sign up here?
By DXS in forum Polling PlazaReplies: 5Last Post: 14th Oct 2009, 04:23 AM -
[PHP] Threading Class
By SplitIce in forum Tutorials and GuidesReplies: 8Last Post: 1st Oct 2009, 01:24 AM
themaLeecher - leech and manage...
Version 5.03 released. Open older version (or...