Howto: Get Number of Rows

Meta: April 08th 2008 // PHP // 346 views

If you have a MySQL table and you want to know how many records there are in this table you can run a query on the entire table for example:

<?php
 
	//setup a query to select all items
	$query = mysql_query("SELECT * FROM `tablename`");
 
	//now use the magic row counter droid
	$rows = mysql_num_rows($query);
 
	echo $rows;
?>

This will show you how many rows were supplied in that query. So if you want to find out how many people are under 18 in your forums you could do something like this:

<?php
 
	$timegap = time() - (18 * 365 * 24 * 60 * 60);
	$query = mysql_query("SELECT * FROM `tablename` WHERE `dob` < '".$timegap."'");
	$rows = mysql_num_rows($query);
	echo $rows;
?>

The time gap gets the time in seconds of 18 years, then subtracts it from the time now. Yay. so now you can limit access to minors :O

WeDecal.com

Postscript: Leave A Comment // Subscribe (RSS Feed)

Want To Be Heard? Comment Here!

// I Love Comments, Especially Nice Ones

Who Are You?

Your Email Address

Your Website

:D :) :o :eek: :( :lol: :wink: :arrow: :idea: :?: :!: :evil: :p

You can follow any responses to this entry via its RSS comments feed. You may also leave a trackback by clicking this link.