By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,614 Members | 1,653 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,614 IT Pros & Developers. It's quick & easy.

Finding out how mnay records are in a queryy

P: n/a
Hi,
Write now I have code to read in a set of data from a surrey. Could I
find out how many rows of data the query has, without adding to a
counter each time I read a row?

Also is there a way to start reading the database from a specific row,
instead of always starting at the begging?

This is what my code looks like
$sql="SELECT * FROM zen_products where 1";

if (!$results=mysql_query($sql,$dblink))
die("<brcould not query database");
// can we figure out the size
// go to a spefic loction without looping?
while( $row=mysql_fetch_array($results))
{
extract($row);
echo "=id";
echo $products_id;
}

// close databse connection
mysql_close($dblink);

Oct 3 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
te*******@gmail.com wrote:
Could I find out how many rows of data the query has, without adding to a
counter each time I read a row?
http://php.net/mysql_num_rows

--
----------------------------------
Iván Sánchez Ortega -ivan-algarroba-sanchezortega-punto-es-

Now listening to: Deep Forest - Music.Detected_ (2002) - [2] Endangered
Species (6:18) (98.333298%)
Oct 3 '08 #2

P: n/a
On 3 Oct, 18:07, Ivn Snchez Ortega <ivansanchez-...@rroba-
escomposlinux.-.punto.-.orgwrote:
tedpot...@gmail.com wrote:
Could I find out how many rows of data the query has, without adding toa
counter each time I read a row?

http://php.net/mysql_num_rows
The mysql buffer lives outside the php memory limit - sometimes it may
be a good idea to get a count of rows without reading them in
So...

depending on your DBMS version:

$SQL="SELECT * FROM zen_products where 1"; // or whatever

$prev_rows=check_cached_count($SQL);
if (!$prev_rows) {
$rows="SELECT COUNT(*) FROM ($SQL)";
$res=mysql_query($rows);
$data=mysql_fetch_row($res);
$prev_rows=$data[0];
save_cache_data($SQL, $prev_rows);
}
....

- this will still hurt the disk I/O on the DBMS though - hence the
caching.

MySQL allows you to first fetch the number of rows then retrieve rows
without hitting the datafiles again by using SQL_CALC_FOUND_ROWS and
FOUND_ROWS() RTFM for more details.

C.
Oct 5 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.