NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming > CODE
Reload this Page Database connection class

CODE This forum is for posting code snippets and example scripts that aren't quite tutorials, but could be useful for others. You may post code snippets and/or completed scripts that you've written and want to share here.

Advanced Search
9 members in live chat ~  


Closed Thread
 
LinkBack Thread Tools
Old 04-08-2007, 03:36 AM THREAD STARTER               #1 (permalink)
NamePros Regular
Join Date: Dec 2005
Posts: 210
wackyjoe is on a distinguished road
 



Database connection class


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

———————————————————————————–

PHP Code:
<?
    
include(”constants.php”);

    class 
MySQLDB
    
{
    var 
$connection;
    var 
$num_active_users;
    var 
$num_active_guests;
    var 
$num_members;

    function 
MySQLDB() {
    
$this->connection mysql_connect(HOSTUSERNAMEPASSWORD);
    
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,unamenumVALUES (’$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 `usersSET `”.$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) {
????: NamePros.com http://www.namepros.com/code/314232-database-connection-class.html
    
$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) {
????: NamePros.com http://www.namepros.com/showthread.php?t=314232
    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 Code:
    <?
    define 
(HOST“localhost”);
    
define (USERNAME“username”);
    
define (PASSWORD“”);
    
define (DATABASE“ds_name”);

    
define(ALL_LOWERtrue);
    
define(MIN_USERNAME4);
    
define(MAX_USERNAME13);

    
define(ADMIN“name”);
    
define(A_LEVEL10);
    
define(M_LEVEL7);
    
define(U_LEVEL1);
    
define(GUEST“Guest”);
    
define(G_LEVEL0);

    
define(WELCOME_EMAILfalse);
    
define(EMAIL_ADDRESS“none@none.com”);

    
define(MIN_NEWS_COMM30);
    
define(MAX_NEWS_COMM300);

    
define(USER_TIME10);
    
define(GUEST_TIME5);

    
define(”USER_TIMEOUT”10);
    
define(”GUEST_TIMEOUT”5);

    
define(”COOKIE_EXPIRE”60*60*24*100);
    
define(”COOKIE_PATH”/);
    
?>
——————————————————————————————–
wackyjoe is offline  
Old 04-11-2007, 12:04 AM   #2 (permalink)
NamePros Member
Join Date: Feb 2007
Location: Surabaya - Indonesia
Posts: 27
silentwind is an unknown quantity at this point
 



hmm, I'm prefer using adodb class from http://adodb.sf.net
__________________
-- Silent's Spot [blog]
-- Curriculum Vitae [CV]
silentwind is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


 
All times are GMT -7. The time now is 03:43 PM.

Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger