11-19-2017, 06:08 +0100
AuthorPost
Turbo  07-29-2007, 19:30   | mysqlmod (+ eggdrop) MSN  Homepage
Moderator
Member since 06/2007
5 Posts
Location: Netherlands
[Eggdrop-TCL]
This works with mysqlmod (http://barkerjr.net/irc/eggdrop/modules/)
                                                                                                                              
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
namespace eval database {

    # -- Mysql Info

    variable dbhost "127.0.0.1"
    variable dbuser "root"
    variable dbpass "secretpass"
    variable dbname "userlist"
    variable dbconnect ""

    # Leave dbconnect empty!

    # --- Module

    loadmodule mysql

    bind evnt - "init-server" [namespace current]::dbconnect

}

Connecting to the database
                                                                                                                              
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
proc database::dbconnect init-server {
    putlog "Connecting to MySQL Server:"
    if {[catch { set database::dbconnect [mysql_connect $database::dbname $database::dbhost $database::dbuser $database::dbpass] } error]} {
        putlog "MySQL Error:"
        foreach x [split $error \n] {
            putlog "$x"
        }
        putlog "End of error."
    } else {
        putlog "Connected to MySQL."
        putlog "Database: $database::dbname"
        mysql_query "CREATE TABLE IF NOT EXISTS `userlist` ( `nickname` varchar(100) NOT NULL, `hostname` varchar(100) NOT NULL )"

    }
}

Check if user is present in the database
                                                                                                                              
1
2
3
4
5
6
7
8
proc isadded {nick} {
    set nick_ident *!*@[lindex [split [getchanhost $nick] "@"] 1]
    if {[mysql_query "SELECT `nickname` FROM `userlist` WHERE `hostname` = '[mysql_escape $nick_ident]'"] == ""} {
        return "0"
    } else {
        return "1"
    }
}

returns 0 if the user was not added
returns 1 if the user was successfully added.



Insert user into the table
                                                                                                                              
1
2
3
4
5
6
7
8
9
proc addnick {nick} {
    set nick_ident *!*@[lindex [split [getchanhost $nick] "@"] 1]
    if {[isadded $nick] == "1"} {
        return "allready added"
    } else {
        mysql_query "INSERT INTO `userlist` ( nickname , hostname  ) VALUES ( '[mysql_escape $nick]' , '[mysql_escape $nick_ident]'  )"
        return "added you in the tabel"
    }
}

Getting hostname from nickname, who is added in the tabel. returns 0 if he is not added
                                                                                                                              
1
2
3
4
5
6
7
8
proc hostname {nick} {
    set nick_ident *!*@[lindex [split [getchanhost $nick] "@"] 1]
    if {[set hostname1 [mysql_query "SELECT `hostname` FROM `userlist` WHERE `hostname` = '[mysql_escape $nick_ident]'"]] == ""} {
        return "0"
    } else {
        return "[join $hostname1]"
    }
}

Delete a user from a table
                                                                                                                              
1
2
3
4
5
6
7
8
9
proc delnick {nick} {
    set nick_ident *!*@[lindex [split [getchanhost $nick] "@"] 1]
    if {[isadded $nick] == "0"} {
        return "didn`t found $nick in the database"
    } else {
        mysql_query "DELETE FROM `userlist` WHERE `nickname` = '[mysql_escape $nick]' AND `hostname` = '[mysql_escape $nick_ident]';"
        return "Deleted you from database"
    }
}

now you see how to connect to a database & add/del/check from a table and creating a table
This post was edited 10 times, last on 06-04-2008, 02:14 by Turbo
Advanced options for this topic:

Ignore this topic (Do not list this topic in the "unread topics" search. You are currently not ignoring this topic.)
Hide this topic (Hidden topics are not displayed in the topics list. This topic is currently not hidden.)
Go to forum

Unclassified NewsBoard 1.5.3-d | © 2003-4 by Yves Goergen