Jump to content


Photo

count function


  • Please log in to reply
4 replies to this topic

#1 jpratt

jpratt
  • Members
  • PipPipPip
  • Advanced Member
  • 144 posts

Posted 27 October 2006 - 06:07 PM

i am using the count function to return the number of rows returned by a query, but it keeps returning 1 even when I know the value i am using has hundereds of records to count. My code looks like:

<?php
$keyword = $_GET['keyword'];
mysql_select_db($database_database, $linkID);
$query = "SELECT * FROM ppctrack WHERE ppcKeyword = " . $keyword;
$result = mysql_query($query, $linkID);
$pageview = count($result);
echo $pageview;
?>

Anyone know what i am doing wrong?

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 27 October 2006 - 06:22 PM

$result does not hold the results but a result resource. A result resource points to the location in the memory where the results are stored. YOu use the result resource with othe mysql functions such  as mysql_fetch_array, mysql_num_rows, mysql_result etc.

What you'll want to do is to use a function called mysql_num_rows this will then return the number of rows returned from the query:
$pageview mysql_num_rows($result);

You only use count on arrays. count then counts the number of items within the array.

#3 jpratt

jpratt
  • Members
  • PipPipPip
  • Advanced Member
  • 144 posts

Posted 27 October 2006 - 06:53 PM

i changed it and still does not work. now i accually gives me an error.

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource

This is what i changed my code to:

<?php
$keyword = $_GET['keyword'];
mysql_select_db($database_database, $linkID);
$query = "SELECT * FROM ppctrack WHERE ppcKeyword = " . $keyword;
$result = mysql_query($query, $linkID);
$pageview = mysql_num_rows($result);
echo $pageview;
?>

Any ideas??

#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 27 October 2006 - 06:59 PM

You need to enclose all strings in quotes in the query.

Try:
<?php
$query = "SELECT * FROM ppctrack WHERE ppcKeyword = '" . $keyword . "'";
$result = mysql_query($query) or die("Problem with the query: $query<br>" . mysql_error());
?>
The "or die" clause will tell you any syntax errors you have with the query.

Ken

#5 jpratt

jpratt
  • Members
  • PipPipPip
  • Advanced Member
  • 144 posts

Posted 27 October 2006 - 07:20 PM

duh, brain freeze, thanks




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users