It works for me, if I add username and password to your connect statement.

You say that your script produces no value. Does it mean that the $total variable is empty? It is not.

It looks like what you posted is not the actual code you're using. Therefore, a few questions are necessary:

Furthermore, the $sth->rows method doesn't return the number of rows in a table, but the number of rows affected by your statement, when your SQL contains modifying commands such as UPDATE, INSERT, DELETE.

That method returns sometimes the number of records from a SELECT statement, but it is driver dependant, and not reliable, as you can see from the DBI docs. Using MySQL, it should return the number of rows if you execute a SELECT that fetches all the rows in your table, but a simple COUNT is less expensive.

To count the number of rows, then, you need to use COUNT(*), as you did.

Please read the above Tutorials, and then provide more information about your case.

Update
The error is NOT in the "AS" keyword as stated by ronzomckelvey. Such keyword is perfectly legal and functional in MySQL. (Updated again - OK, I see that both Zaxo and dws have pointed out that in detail.)

_ _ _ _ (_|| | |(_|>< _|

In reply to Re: SELECT COUNT and DBI: rows by gmax
in thread SELECT COUNT and DBI: rows by perleager

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.