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.

Count msyql output

Help for those learning Tcl or writing their own scripts.
Post Reply
M
MUSUL
Voice
Posts: 5
Joined: Mon Apr 26, 2010 4:02 pm

Count msyql output

Post by MUSUL »

Hello,

How can count all the results from a sql query? Im trying this:

Code: Select all

set sql "SELECT id FROM news"
	set query [mysql::query $db_handle $sql]
	
	if {[set row [mysqlnext $query]] != ""} { incr n 1 }
	
	putquick "PRIVMSG #test:$n news registered."
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Either use the COUNT() SQL-function in your query to return the number of matching records, or consider using the ::mysql::result command with the "rows" option
NML_375
M
MUSUL
Voice
Posts: 5
Joined: Mon Apr 26, 2010 4:02 pm

Post by MUSUL »

ok ty, working with mysql::result. Now i have another doubt.

Using rand function like this:

set n [rand 5]

what's the range for the rand? 1-5? 1-4?
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

0 through n-1, where n is the first argument on the command line.
NML_375
r
raider2k
Op
Posts: 140
Joined: Tue Jan 01, 2008 10:42 am

Post by raider2k »

Code: Select all

SELECT COUNT(*) FROM `news`
is most probably the best idea since selecting all rows, putting them into list and then incr every entry is a waste of data and also takes longer

about rand: the syntax is [rand ?maximum?] which means a random number between 0 and ?maximum? is being chosen
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

raider2k:
Please re-read the documentation for rand:
rand <limit>
Returns: a random integer between 0 and limit-1
Module: core
NML_375
r
raider2k
Op
Posts: 140
Joined: Tue Jan 01, 2008 10:42 am

Post by raider2k »

correct

the command applies to random between 0 and limit-1, so if limit is 100, the max value is 99, sorry.
Post Reply