Jump to content

Archived

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

jpratt

count function

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?

Share this post


Link to post
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.

Share this post


Link to post
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??

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

×

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.