05-04-2009, 07:07 PM
|
#33 (permalink)
|
|
The Gregarious
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
|
Quote:
Originally Posted by Tanax
Hello, I'm new to this forum, and I'm not exactly sure if this is the correct place to post this, but I hope it is.
Anyways, I've done 2 classes, a database class that handles the database querys and general stuff with the database, and a member class that manages the members in the database.
And yea, I know that my comment lines are big.
_database.php
PHP Code:
<?php
/**
||||||||||||||||||||||||||||||||||||||||||
|||| @author Tanax
|||| @copyright 2007
||||||||||||||||||||||||||||||||||||||||||
**/
class database {
private $db = array();
/**
||||||||||||||||||||||||||||||||||||||||||
|||| Our constructor that auto-loads
||||||||||||||||||||||||||||||||||||||||||
**/
public function __construct($host, $user, $pass) {
$this->db['host'] = $host;
$this->db['user'] = $user;
$this->db['pass'] = $pass;
$this->connectdb();
}
/**
||||||||||||||||||||||||||||||||||||||||||
|||| Connect to db
||||||||||||||||||||||||||||||||||||||||||
**/
private function connect() {
@mysql_connect($this->db['host'], $this->db['user'], $this->db['pass']) or die('Could not connect to the database!');
}
/**
||||||||||||||||||||||||||||||||||||||||||
|||| Select db
||||||||||||||||||||||||||||||||||||||||||
**/
public function select($name) {
$this->db['db'] = $name;
@mysql_select_db($this->db['db']) or die('This system has not been installed, or something is wrong in the database table!');
}
/**
||||||||||||||||||||||||||||||||||||||||||
|||| Disconnect db
||||||||||||||||||||||||||||||||||||||||||
**/
public function disconnect() {
mysql_close();
}
/**
||||||||||||||||||||||||||||||||||||||||||
|||| Makes a query
||||||||||||||||||||||||||||||||||||||||||
**/
public function query($string) {
$result = mysql_query($string);
return $result;
}
/**
||||||||||||||||||||||||||||||||||||||||||
|||| Fetches a result
||||||||||||||||||||||||||||||||||||||||||
**/
public function fetch($string) {
$result = mysql_fetch_array($string);
return $result;
}
/**
||||||||||||||||||||||||||||||||||||||||||
|||| Makes a value safe from SQL injection
||||||||||||||||||||||||||||||||||||||||||
**/
public function makesafe($string) {
$result = mysql_real_escape_string($string);
return $result;
}
/**
||||||||||||||||||||||||||||||||||||||||||
|||| Installs a system, adding tables
||||||||||||||||||||||||||||||||||||||||||
**/
public function install($system, $tablename) {
if($system == "membersystem") {
$create = "CREATE TABLE `".$tablename."` (
`u_id` int(11) NOT NULL auto_increment,
`username` varchar(25) NOT NULL default '',
`registerdate` int(25) NOT NULL default '',
`registerip` varchar(32) NOT NULL default '',
PRIMARY KEY (`u_id`)
) TYPE=MyISAM AUTO_INCREMENT=60";
}
elseif($system == "memberpm") {
$create = "CREATE TABLE `".$tablename."` (
`id` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`title` VARCHAR( 255 ) NULL,
`message` TEXT NOT NULL,
`from` INT( 11 ) NOT NULL ,
`to` INT( 11 ) NOT NULL ,
`from_viewed` BOOL NOT NULL DEFAULT '0',
`to_viewed` BOOL NOT NULL DEFAULT '0',
`from_deleted` BOOL NOT NULL DEFAULT '0',
`to_deleted` BOOL NOT NULL DEFAULT '0',
`from_vdate` DATETIME NULL,
`to_vdate` DATETIME NULL,
`from_ddate` DATETIME NULL,
`to_ddate` DATETIME NULL,
`created` DATETIME NOT NULL
) ENGINE = MYISAM";
}
$insert = $this->query($create);
if($insert) {
$msg = "Table :" .$tablename. " was successfully created!";
return $msg;
} else {
$msg = "Table: " .$tablename. " could not be created!";
return $msg;
}
}
}
?>
So that was the database class, this is the membersclass.
_members.php
PHP Code:
<?php
/**
||||||||||||||||||||||||||||||||||||||||||
|||| @author Tanax
|||| @copyright 2007
||||||||||||||||||||||||||||||||||||||||||
**/
class member {
private $db;
private $data = array('u_id' => '', 'username' => '', 'password' => '', 'registerdate' => '', 'registerip' => '');
private $table = array();
/**
||||||||||||||||||||||||||||||||||||||||||
|||| Our constructor that auto-loads
||||||||||||||||||||||||||||||||||||||||||
**/
public function __construct(database $db, $tablename) {
$this->db = $db;
$this->table['members'] = $tablename;
}
/**
||||||||||||||||||||||||||||||||||||||||||
|||| Creates, or updates a member
||||||||||||||||||||||||||||||||||||||||||
**/
public function save();
$sql = sprintf('INSERT INTO
%1$s
(username, password, registerdate, registerip)
VALUES
("%2$s", "%3$s", "%4$s", "%5$s")
ON DUPLICATE KEY
UPDATE
username = "%2$s",
password = "%3$s",
registerdate = "%4$s",
registerip = "%5$s"',
$this->table['members'],
$this->data['username'],
$this->data['password'],
$this->data['registerdate'],
$this->data['registerip']);
$this->db->query($sql);
}
/**
||||||||||||||||||||||||||||||||||||||||||
|||| Checks if user is loaded
||||||||||||||||||||||||||||||||||||||||||
**/
public function isLoaded() {
return isset($this->data['id']);
}
/**
||||||||||||||||||||||||||||||||||||||||||
|||| Loads a user by ID
||||||||||||||||||||||||||||||||||||||||||
**/
public function load($u_id) {
$sql = printf("SELECT `u_id`, `username`, `password`, `registerdate`, `registerip` FROM `".$this->table['members']."` WHERE `u_id` = %d", $u_id);
$this->data = $this->db->fetch($sql);
}
/**
||||||||||||||||||||||||||||||||||||||||||
|||| Searches for a user by name
||||||||||||||||||||||||||||||||||||||||||
**/
public function find($name) {
$name = $this->db->makesafe($name);
$sql = printf("SELECT `u_id` FROM `".$this->table['members']."` WHERE `username` = %s", $name);
$id = $this->db->fetch($sql);
// If anything was found
if(isset($id['id'])) {
$this->load($id['id']);
}
}
/**
||||||||||||||||||||||||||||||||||||||||||
|||| Gets ID of loaded user
||||||||||||||||||||||||||||||||||||||||||
**/
public function getId() {
if(!isset($this->data['u_id'])) {
trigger_error('Tries to get property of not loaded user.', E_USER_NOTICE);
return false;
}
return $this->data['u_id'];
}
/**
||||||||||||||||||||||||||||||||||||||||||
|||| Gets name of loaded user
||||||||||||||||||||||||||||||||||||||||||
**/
public function getName() {
if(!isset($this->data['username'])) {
trigger_error('Tries to get property of not loaded user.', E_USER_NOTICE);
return false;
}
return $this->data['username'];
}
/**
||||||||||||||||||||||||||||||||||||||||||
|||| Sets name of user
||||||||||||||||||||||||||||||||||||||||||
**/
public function setName($name) {
$name = $this->data->makesafe($name);
$this->data['username'] = (string) $name;
}
/**
||||||||||||||||||||||||||||||||||||||||||
|||| Gets password of loaded user
||||||||||||||||||||||||||||||||||||||||||
**/
public function getPass() {
if(!isset($this->data['password'])) {
trigger_error('Tries to get property of not loaded user.', E_USER_NOTICE);
return false;
}
return $this->data['password'];
}
/**
||||||||||||||||||||||||||||||||||||||||||
|||| Sets password of user
||||||||||||||||||||||||||||||||||||||||||
**/
public function setPass($pass) {
$pass = $this->data->makesafe($pass);
$this->data['password'] = $pass;
}
/**
||||||||||||||||||||||||||||||||||||||||||
|||| Gets registerdate of loaded user
||||||||||||||||||||||||||||||||||||||||||
**/
public function getRegdate() {
if(!isset($this->data['registerdate'])) {
trigger_error('Tries to get property of not loaded user.', E_USER_NOTICE);
return false;
}
return $this->data['registerdate'];
}
/**
||||||||||||||||||||||||||||||||||||||||||
|||| Sets registerdate of user
||||||||||||||||||||||||||||||||||||||||||
**/
public function setRegdate($date) {
$this->data['registerdate'] = $date;
}
/**
||||||||||||||||||||||||||||||||||||||||||
|||| Gets registerip of loaded user
||||||||||||||||||||||||||||||||||||||||||
**/
public function getRegip() {
if(!isset($this->data['registerip'])) {
trigger_error('Tries to get property of not loaded user.', E_USER_NOTICE);
return false;
}
return $this->data['registerip'];
}
/**
||||||||||||||||||||||||||||||||||||||||||
|||| Sets registerip of user
||||||||||||||||||||||||||||||||||||||||||
**/
public function setRegip($ip) {
$this->data['registerip'] = $ip;
}
}
?>
So I'm just wondering if you see any errors, or if you see anything that could be done better/safer.
|
Hey Tanax,
Is this code still good or have you updated it to something better?
I'm sure your OO skills have improved since 2007!!!

|
|
|
|