HOW TO COUNT ROWS FROM SELECT STATEMENTS!!!!!!

From: Date: Wed, 13 Sep 2000 01:40:47 +0000
Subject: HOW TO COUNT ROWS FROM SELECT STATEMENTS!!!!!!
Groups: php.db 
Request: Send a blank email to php-db+get-2850@lists.php.net to get a copy of this message
Hi all, This has been asked about 4-5 times recently. FAQ? 1) append "count(*)" to the front of your query. Then you know that the first column in your rowset is the rowcount. 2) while (odbc_fetch_row()) $count++; This is a bit evil. Only do it on real small queries, as it's REALLY slow. Also remember to fetch_row(1) afterwards if your cursor supports it. Passing SQL_CUR_USE_ODBC to odbc_connect will give you a scrolling cursor, but this way is still evil. 3) This is another way to count rows. We use this in an ODBC class. It may be slow on large queries, but you get that. It's *much* quicker than the above way (2). ////////////////////////////////////////////////////////// function countRows($str, $con) { //replace "select <list>" with "select count(*)" $var = explode("from", $str); $sql = "select count(*) from " . $var[1]; //Get rid of group by/order by $var = explode("group", $sql); $var = explode("order", $var[0]); $sql = $var[0]; file://count rows returned by select statement $count = odbc_exec($con, $sql); odbc_fetch_row($count, 1); $rowcount = odbc_result($count, 1); odbc_free_result($count); return $rowcount; } ////////////////////////////////////////////////////////// These are probably all simplistic and inefficient ways to do this, but keep in mind: it is a hack. One way to describe hacking is "getting a program to do something it's not supposed to." My advice is: don't count rows. If you have to...hope the above helps. Regards, Tyson Lloyd Thwaites IT&e Ltd

« previous php.db (#2850) next »