Jump to content

count function


jpratt

Recommended Posts

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:

[code]
<?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;
?>
[/code]

Anyone know what i am doing wrong?
Link to comment
https://forums.phpfreaks.com/topic/25325-count-function/
Share on other sites

$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:
[code]$pageview mysql_num_rows($result);[/code]

You only use count on arrays. count then counts the number of items within the array.
Link to comment
https://forums.phpfreaks.com/topic/25325-count-function/#findComment-115485
Share on other sites

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:

[code]
<?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;
?>
[/code]

Any ideas??
Link to comment
https://forums.phpfreaks.com/topic/25325-count-function/#findComment-115510
Share on other sites

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

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

Ken
Link to comment
https://forums.phpfreaks.com/topic/25325-count-function/#findComment-115514
Share on other sites

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.