- Impact
- 3
I wrote this database connection code a long time ago. It was meant for a small portal community, so therefore there will be some bits that may serve no sort of purpose.
But feel free to use it.
———————————————————————————–
mysql.php
———————————————————————————–
———————————————————————————–
———————————————————————————–
constants.php
———————————————————————————–
——————————————————————————————–
———————————————————————————–
mysql.php
———————————————————————————–
PHP:
<?
include(”constants.php”);
class MySQLDB
{
var $connection;
var $num_active_users;
var $num_active_guests;
var $num_members;
function MySQLDB() {
$this->connection = mysql_connect(HOST, USERNAME, PASSWORD);
mysql_select_db(DATABASE, $this->connection) or die(mysql_error());
$this->numMember();
$this->calcGuests();
}
function delShout($id) {
$info = mysql_query(”DELETE FROM shoutbox WHERE id=’$id’”, $this->connection);
if ($info) {
return true;
}else{
return false;
}
}
function numMember() {
$info = mysql_query(”SELECT * FROM users”, $this->connection);
$num = mysql_num_rows($info);
$this->num_members = $num;
}
function calcGuests() {
$info = mysql_query(”SELECT * FROM guests”, $this->connection);
$num = mysql_num_rows($info);
$this->num_active_guests = $num;
}
function addGuest($ip, $time) {
$info = mysql_query(”REPLACE INTO guests VALUES (’$ip’, ‘$time’)”, $this->connection);
$this->calcGuests();
}
function removeActiveGuest ($ip) {
$info = mysql_query(”DELETE FROM guests WHERE ip=’$ip’”, $this->connection);
$this->calcGuests();
}
function addNewsComment($comm, $user, $id) {
$info = mysql_query(”INSERT INTO newscomment (comment,uname, num) VALUES (’$comm’, ‘$user’, ‘$id’)”, $this->connection) or die(mysql_error());
if ($info) {
return true;
}else{
return false;
}
}
function calcActiveUsers() {
$info = mysql_query(”SELECT * FROM active_users”) or die(mysql_error());
$num = mysql_num_rows($info);
$this->num_active_users = $num;
}
function addActiveUser($uname, $time) {
$info = mysql_query(”REPLACE INTO active_users VALUES (’$uname’, ‘$time’)”, $this->connection) or die(mysql_error());
$this->calcActiveUsers();
}
function removeInactiveUsers(){
$time = time()-USER_TIME*60;
mysql_query(”DELETE FROM active_users WHERE time < $time”, $this->connection);
$this->calcActiveUsers();
}
function removeInactiveGuests(){
$timeout = time()-GUEST_TIME*60;
mysql_query(”DELETE FROM guests WHERE time < $timeout”, $this->connection);
$this->calcGuests();
}
function removeActiveUser($uname) {
$info = mysql_query(”DELETE FROM active_users WHERE username=’$uname’”, $this->connection) or die(mysql_error());
$this->calcActiveUsers();
}
function updateUserField ($uname, $field, $value) {
$info = “UPDATE `users` SET `”.$field.”` = ‘$value’ WHERE `username`=’$uname’”;
return mysql_query($info, $this->connection) or die(mysql_error());
}
function userExist($uname) {
$info = mysql_query(”SELECT * FROM users WHERE username=’$uname’”, $this->connection) or die(mysql_error());
$num = mysql_num_rows($info);
if ($num == 0) {
return 0;
}else {
return 1;
}
}
function confirmID($name, $id) {
$info = mysql_query(”SELECT * FROM users WHERE username=’$name’”, $this->connection) or die(mysql_error());
if (!$info || mysql_num_rows($info) == 0) {
return 1;
}
$r = mysql_fetch_array($info);
$uid = $r[’userid’];
if ($id == $uid) {
return 0;
}else{
return 2;
}
}
function getInfo($uname) {
$info = mysql_query(”SELECT * FROM users WHERE username=’$uname’”, $this->connection) or die(mysql_error());
if (!$info || mysql_num_rows($info) == 0) {
return NULL;
}
$array = mysql_fetch_array($info);
return $array;
}
function confirmUser($uname, $pass) {
$info = mysql_query(”SELECT * FROM users WHERE username=’$uname’”, $this->connection) or die(mysql_error());
if (mysql_num_rows($info) == 0) {
return 1;
}
$r = mysql_fetch_array($info);
$upass = $r[’password’];
if ($pass != $upass) {
return 2;
}else {
return 0;
}
}
function register($uname, $pass, $email, $check, $msn, $yahoo, $icq, $aim, $website, $location, $name, $lname)
{
if (strtolower($uname) == ADMIN) {
$level = 10;
}
else {
$level = 1;
}
$q = “INSERT INTO `users` (`username`, `password`, `email`, `show`, `msn`, `yahoo`, `icq`, `aim`, `website`, `location`, `name`, `lname`, `userlevel`) VALUES (’$uname’,'$pass’, ‘$email’, ‘$check’, ‘$msn’, ‘$yahoo’, ‘$icq’, ‘$aim’, ‘$website’, ‘$location’, ‘$name’, ‘$lname’, ‘$level’)”;
return mysql_query($q, $this->connection) or die(mysql_error());
}
function query($query){
return mysql_query($query, $this->connection);
}
}
$database = new MySQLDB;
?>
———————————————————————————–
———————————————————————————–
constants.php
———————————————————————————–
PHP:
<?
define (HOST, “localhost”);
define (USERNAME, “username”);
define (PASSWORD, “”);
define (DATABASE, “ds_name”);
define(ALL_LOWER, true);
define(MIN_USERNAME, 4);
define(MAX_USERNAME, 13);
define(ADMIN, “name”);
define(A_LEVEL, 10);
define(M_LEVEL, 7);
define(U_LEVEL, 1);
define(GUEST, “Guest”);
define(G_LEVEL, 0);
define(WELCOME_EMAIL, false);
define(EMAIL_ADDRESS, “[email protected]”);
define(MIN_NEWS_COMM, 30);
define(MAX_NEWS_COMM, 300);
define(USER_TIME, 10);
define(GUEST_TIME, 5);
define(”USER_TIMEOUT”, 10);
define(”GUEST_TIMEOUT”, 5);
define(”COOKIE_EXPIRE”, 60*60*24*100);
define(”COOKIE_PATH”, “/”);
?>
——————————————————————————————–





