Jump to content

[SOLVED] mysql_num_rows using limit in sql command


ahs10

Recommended Posts

i have a query that uses LIMIT, but i'd like to get the mysql_num_rows() of that query if the limit wasn't there.  what's the most efficient way to do that?

 

my only solution is...

 

$query = "SELECT * FROM table";

$rcResult = mysql_query($query);

$realCount = mysql_num_rows($rcResult);

$query .= " LIMIT 5";

$result = mysql_query($query);

 

.... i didn't know if there was something more clever than that.

 

any help, guidance, advice, or simple hellos are much appreciated.  thanks!

 

 

Well, if you are already going to execute the query to get the number of rows, why wouldn't you just use that resource and LIMIT with PHP?

 

<?php
  $limit = 5;
  $query = "SELECT * FROM table";
  $result = mysql_query($query);
  $count = mysql_num_rows($result);
  for($i =0;$i < $limit && $row = mysql_fetch_array($result);$i++){
    print_r($row);
  }
?>

Why don't you just ALWAYS use the LIMIT. If there are less records than the limit then it will only pull those. Then just use a mysql_num_rows() after the query to determine how many were actually returned. I don't understand why you need to know the number of records to determine what limit to use.

 

$limit = 5;
$query = "SELECT * FROM table LIMIT $limit";
$rcResult = mysql_query($query);
$realCount = mysql_num_rows($rcResult);
echo "Query limit: $limit<br>Returned count: $realCount";

that's what i get for trying to simplify my problem... i apologize.

 

i'm doing a paging feature, but executed via ajax.  i need to use the offset and maximum parameters that come with sql's limit, but also know how many rows are in the query without the limit.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.