Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Count returning incorrect results

Posted on 2009-04-11
6
201 Views
Last Modified: 2012-05-06
I'm using the query below and the query itself is working fine.  The contents of the results is working as expected.

What's not working is the count.  I get different count results if I run the query in a web page and then the same query in mySQL Administrator.  

What I'm trying to do here is have the output do something different when it gets to the last item in the recordset.  But I can't seem to get the count correct.

Perhaps there is a different method all together?
$query = "SELECT exp_category_posts.entry_id, exp_weblog_titles.url_title, exp_weblog_titles.title FROM exp_category_posts JOIN exp_weblog_titles ON exp_category_posts.entry_id = exp_weblog_titles.entry_id WHERE (exp_category_posts.cat_id = '" . $cid . "' AND exp_weblog_titles.status = 'open')";
$rs = mysql_query($query);
 
$count = count(mysql_fetch_assoc($rs));  //incorrect results
 
$i = 1;
 
//reset the pointer
mysql_data_seek($rs, 0);
 
// output the results
if ($rs) {
  while($rw = mysql_fetch_assoc($rs)) {
    //if (end(mysql_fetch_assoc($RS))  //doesn't work
    if ($i == $count)
    {
    echo "<li class='last'>" . $rw['title'] . "</li>";
    }
    else
    {
    echo "<li>" . $rw['title'] . "</li>";
    }
 
    $i++;
  }
}

Open in new window

0
Comment
Question by:mcnuttlaw
6 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 24121817
mysql_fetch_assoc returns an array of the first row, so count() of that returns the number of columns, and not the number of rows
$query = "SELECT exp_category_posts.entry_id, exp_weblog_titles.url_title, exp_weblog_titles.title FROM exp_category_posts JOIN exp_weblog_titles ON exp_category_posts.entry_id = exp_weblog_titles.entry_id WHERE (exp_category_posts.cat_id = '" . $cid . "' AND exp_weblog_titles.status = 'open')";
$rs = mysql_query($query);
$count = mysql_num_rows($rs));  
$i = 1  
while($rw = mysql_fetch_assoc($rs)) 
{
    if ($i == $count)
    {
      echo "<li class='last'>" . $rw['title'] . "</li>";
    }
    else
    {
      echo "<li>" . $rw['title'] . "</li>";
    }
 
    $i++;
  }
}

Open in new window

0
 
LVL 2

Author Comment

by:mcnuttlaw
ID: 24121849
mysql_num_rows($rs) is returning 1 on all queries.  

Correct me if I'm wrong but isn't $rs in this case simply a 'raw dump' of the query and mysql_fetch_assoc 'splits' it into a usable array?
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24121891
>mysql_num_rows($rs) is returning 1 on all queries.  
I cannot believe you on that, unless you have a LIMIT 1 in all your queries...

please double check
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24121892
and $rs is just the resource handler, not a "raw dump" of the query results
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 24122538
>mysql_num_rows($rs) is returning 1 on all queries.  

Actually, it is expected to be a "resource id".  However it will look like a "1" or TRUE if the query succeeded, as opposed to a "0" or FALSE if the query failed.

Use var_dump($rs) to see what it looks like.

Either of these methods should get you a count of rows in the results set, put into the variable $num.  Note that this may not be the same as the number of rows in the table(s), if you use a WHERE or LIMIT clause to down-select from the total rows available.

HTH, ~Ray
$qry = "SELECT * FROM myTable";
$res = mysql_query($qry);
if (!$res) die( mysql_error() );
$num = mysql_num_rows($res); // HOW MANY ROWS IN RESULTS SET?
 
 
$qry = "SELECT count(*) AS kount FROM myTable";
$res = mysql_query($qry);
if (!$res) die( mysql_error() );
$row = mysql_fetch_assoc($res);
$num = $row["kount"]; // HOW MANY ROWS IN RESULTS SET?

Open in new window

0
 
LVL 8

Expert Comment

by:aldanch
ID: 24133672

$q = mysql_query("select * from myTable");
if(mysql_num_rows($q) != "0")
{
     echo "I found ". mysql_num_rows($q) ." records\n";
 
     while($row = mysql_fetch_assoc($q))
     {
          $whatever = $row["whatever"];
          ...
     }
} else {
     echo "I got nothin' for ya.\n";
}

Open in new window

0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Fulfillment API php code sample 1 58
comma true 6 37
Wordpress French and English Site 6 77
Extracting store locations from Google maps or site 2 22
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question