List:General Discussion« Previous MessageNext Message »
From:Paul DuBois Date:October 31 2001 1:41am
Subject:Re: HOW do I return the results of a count to a variable
View as plain text  
At 12:43 AM +0000 10/31/01, Mark Worsdall wrote:
>Hi,
>
>I want to alter this statement so it returns the number of records.
>
>$prep = "SELECT COUNT(*) FROM go.objectives WHERE subjects_id='1' 
>AND subjectHeadings_id='2' ORDER BY displayOrder";
>
>$sth = $dbh->prepare($prep);
>
>$sth->execute;
>
>So what I need to do is add the COUNT statement into the above and 
>then return the number of records but how?

That makes no sense to me.  The statement already includes COUNT(*) and
thus returns the number of rows that match the conditions in the WHERE
clause.  (By the way, the ORDER BY is completely superfluous, because
the query returns only one row anyway.)

However, in the code shown above, all you've done is execute the query.
You haven't fetched the result.  This should do it:

$count = $sth->fetchrow_array();

You could do this in a single step:

$count = $dbh->selectrow_array (
     "SELECT COUNT(*) FROM go.objectives WHERE subjects_id='1' AND
      subjectHeadings_id='2' ORDER BY displayOrder");


>
>MySQL table query
>
>
>M.
>--
>Work:- postmasterAThinwick.demon.co.uk  WEB:- http://www.hinwick.demon.co.uk
>Work:- mworsdallATshaftesburysoc.org.uk REPLACE AT with @
>Home:- hinwickATworsdall.demon.co.uk    WEB:- http://www.worsdall.demon.co.uk
>Shadow:- webmasterATshadow.org.uk       WEB:- http://www.shadow.org.uk
Thread
(errno: 40)Eric Dean29 Oct
  • Re: (errno: 40)mickalo29 Oct
  • HOW do I return the results of a count to a variableMark Worsdall31 Oct
    • Re: HOW do I return the results of a count to a variablemickalo31 Oct
    • Re: HOW do I return the results of a count to a variablemickalo31 Oct
    • Re: HOW do I return the results of a count to a variablePaul DuBois31 Oct
      • Re: HOW do I return the results of a count to a variableMark Worsdall31 Oct
        • Re: HOW do I return the results of a count to a variablePaul DuBois1 Nov
  • Having trouble using LIMITMark Worsdall31 Oct
    • Re: Having trouble using LIMITSteve Meyers31 Oct
      • Re: Having trouble using LIMITMark Worsdall31 Oct
    • Re: Having trouble using LIMITPaul DuBois31 Oct
  • Re: HOW do I return the results of a count to a variableAnne Shroeder31 Oct
  • Re: HOW do I return the results of a count to a variableBill Adams1 Nov
    • Re: HOW do I return the results of a count to a variablePaul DuBois1 Nov
  • Re: HOW do I return the results of a count to a variableHarald Fuchs1 Nov
    • Re: HOW do I return the results of a count to a variablePaul DuBois2 Nov