Forum Moderators: coopster

Message Too Old, No Replies

Select COUNT() Question?

         

angst

3:55 pm on Jul 8, 2005 (gmt 0)

10+ Year Member



Hello,

I'm trying to count the total # of records called using:

$num_result = mysql_query("SELECT COUNT(*) FROM picdata where PicSection= '$view' LIMIT $current,$maxresults ") or exit(mysql_error());
echo $num_result;

and it kind of works by giving me back:

Resource id #4

but i only want the number and not the additional text.

how can this be done?

thanks in advance for your time!
-Ken

gliff

4:04 pm on Jul 8, 2005 (gmt 0)

10+ Year Member



The function mysql_query does not return any fields. It returns a reference to a result set. You must use one of the fetch functions to get an actual row back. (untested code, just to give you an idea of what to do)

$query = mysql_query("SELECT COUNT(*) as total FROM picdata where PicSection= '$view' LIMIT $current,$maxresults ") or exit(mysql_error());

$theRow = mysql_fetch_array($query, MYSQL_ASSOC);

$num_results = $theRow['total'];

angst

4:20 pm on Jul 8, 2005 (gmt 0)

10+ Year Member



hmm, thats giving me an error,

this is my code so far:

$link = mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database");
$db_selected = mysql_select_db($DBName, $link) or die('Database name incorrect');

$num_result = mysql_query("SELECT COUNT(*) FROM picdata where PicSection= '$view' LIMIT $current,$maxresults ") or exit(mysql_error());

any ideas?

thanks again!
-Ken

dreamcatcher

4:27 pm on Jul 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$num_result = mysql_query("SELECT COUNT(*) as total_count FROM picdata where PicSection= '$view' LIMIT $current,$maxresults ") or exit(mysql_error());

$row = mysql_fetch_object($num_result);

echo $row->total_count;

dc

angst

4:49 pm on Jul 8, 2005 (gmt 0)

10+ Year Member



hmm, but that seems to give me the total count of all records of that type, insted of the number of records currently being displayed?

and notice the LIMIT,
the values would be:
$current = 9
$maxresults = 9

so the result should only be 9, right now it's showing 25. is there a way to just show the number of records being called?

thanks again!
-Ken

mogenshoj

9:06 pm on Jul 8, 2005 (gmt 0)

10+ Year Member



$row_count = mysql_num_rows($num_result);

print "$row_count";

?