This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

Returning Results

Help for those learning Tcl or writing their own scripts.
Post Reply
d
danwa
Voice
Posts: 2
Joined: Mon Oct 17, 2011 11:52 pm

Returning Results

Post by danwa »

Using this in conjunction with the mysql eggdrop module

http://www.barkerjr.net/eggdropmodules.html
http://wiki.barkerjr.net/wiki/MySQL_Module#Tcl_Commands

Code: Select all

proc mysql {n u h c a} {
mysql_connect dbname dbhost dbuser dbpass
mysql_query "select * from Residential WHERE Phone1 = 'a'"
puthelp "PRIVMSG $c: $a"
mysql_errno
}
bind pub -|- !mysqlquery mysql
In the Table Residential there is a field named Phone1 that is populated in every row.

I can't get it to return anything, saying that, i'm not very confident with TCL.
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

The script isn't working cos you forgot to fetch the results after you've queried the database. Don't know your database structure so I will fetch first 3 rows of the database and you should change this.

Code: Select all

set db(host) "localhost"
set db(user) "user"
set db(pass) "secret"
set db(dbase) "search"

bind pub -|- !mysqlquery mysql

proc mysql {nick uhost hand chan txt} {
	global db
	set db_handle [mysqlconnect -host $db(host) -user $db(user) -password $db(pass) -db $db(dbase)]
	set results [mysqlquery $db_handle "SELECT * from Residential WHERE Phone1 = 'a'"]
	if {![moreresult $results]} {
		puthelp "PRIVMSG $chan :Couldn't find any matches."
	} else {
		puthelp "PRIVMSG $chan :Matches:"
		while {[set row [mysqlfetch $results]] != ''} {
			set row1 [lindex $row 0]
			set row2 [lindex $row 1]
			set row3 [lindex $row 2]
			puthelp "PRIVMSG $chan :row1: $row1, row2: $row2, row3: $row3"
		}
		mysqlendquery $results
	}
	mysqlclose $db_handle
}
Haven't tested this but in theory should work, if you change the database info and the rows. Let me know if it doesn't. ;)
Once the game is over, the king and the pawn go back in the same box.
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

The code posted by caesar is written for the mysqltcl tcl module, not BarkerJr's eggdrop module. Personally, I'd recommend the mysqltcl module as well, as it's a bit more flexible, and remains the same across other tcl-based applications.

That said, the problem with your script, is that you don't use the return-value of the mysql_query command:

Code: Select all

...
mysql_query "select * from Residential where Phone1 = 'a'"
..
To store the result in a variable, use this:

Code: Select all

set result [mysql_query "select * from Residential where Phone1 = 'a'"]
Now you can send the result to the channel, one record at a time:

Code: Select all

foreach item $result {
  puthelp "PRIVMSG $c :Record: $item"
## Or perhaps a little neater:
# puthelp "PRIVMSG $c :Record: [join $item ", "]"
}
NML_375
d
danwa
Voice
Posts: 2
Joined: Mon Oct 17, 2011 11:52 pm

Post by danwa »

Thanks for letting me know guys :)
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

Thanks nml375. :) I forgot to mention that.
Once the game is over, the king and the pawn go back in the same box.
Post Reply