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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.