displaying the result of a COUNT query in MySQL

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • briansmccabe@gmail.com

    #1

    displaying the result of a COUNT query in MySQL

    Does anyone have a good approach to displaying in PHP a simple COUNT
    query that is performed on a table in a MySQL db?

    Thanks

  • Kimmo Laine

    #2
    Re: displaying the result of a COUNT query in MySQL

    <briansmccabe@g mail.com> kirjoitti
    viestissä:11350 16969.513258.27 6220@g44g2000cw a.googlegroups. com...[color=blue]
    > Does anyone have a good approach to displaying in PHP a simple COUNT
    > query that is performed on a table in a MySQL db?
    >[/color]


    $result = mysql_query('SE LECT COUNT(*) AS foo FROM table') or
    die(mysql_error ());
    $bar = mysql_fetch_arr ay($result);
    echo $bar['foo'];

    --
    SETI @ Home - Donate your cpu's idle time to science.
    Further reading at <http://setiweb.ssl.ber keley.edu/>
    Kimmo Laine <antaatulla.sik anautaa@gmail.c om.NOSPAM.inval id>


    Comment

    • briansmccabe@gmail.com

      #3
      Re: displaying the result of a COUNT query in MySQL

      my code:

      $query6 = "SELECT COUNT (movie_id) as quant FROM movies WHERE divx = 1
      AND format = 'reg'";
      $result6 = mysql_fetch_arr ay($query6);

      later on the page:

      echo $result6[quant]

      result:

      Warning: mysql_fetch_arr ay(): supplied argument is not a valid MySQL
      result resource in /home/tgupc/public_html/admin/divxreport.php on line
      19


      Any suggestions?

      thanks

      Comment

      • Andy Hassall

        #4
        Re: displaying the result of a COUNT query in MySQL

        On 19 Dec 2005 11:50:43 -0800, briansmccabe@gm ail.com wrote:
        [color=blue]
        >my code:
        >
        >$query6 = "SELECT COUNT (movie_id) as quant FROM movies WHERE divx = 1
        >AND format = 'reg'";
        >$result6 = mysql_fetch_arr ay($query6);
        >
        >later on the page:
        >
        >echo $result6[quant]
        >
        >result:
        >
        >Warning: mysql_fetch_arr ay(): supplied argument is not a valid MySQL
        >result resource in /home/tgupc/public_html/admin/divxreport.php on line
        >19[/color]

        Kimmo posted code with basic error handling, which you've removed. Put it back
        in again and it'll tell you why it failed.

        --
        Andy Hassall :: andy@andyh.co.u k :: http://www.andyh.co.uk
        http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool

        Comment

        • briansmccabe@gmail.com

          #5
          Re: displaying the result of a COUNT query in MySQL

          I put it back in, and the exact same result occurs.

          FYI, line 19 is as follows:

          $result6 = mysql_fetch_arr ay($query6);

          Comment

          • Andy Hassall

            #6
            Re: displaying the result of a COUNT query in MySQL

            On 19 Dec 2005 12:01:34 -0800, briansmccabe@gm ail.com wrote:
            [color=blue]
            >[/color]

            Please quote some context when replying. The default "reply" at the bottom in
            Google Groups cuts out all previous text; this is not the accepted way to post
            to Usenet. Click "show options" next to the author/date for the message, then
            use the "Reply" option there; this quotes and attributes the previous message.
            Then follow:



            ... i.e. don't just quote the lot, unless the message is small and the whole
            message is relevant to your reply.
            [color=blue]
            >I put it back in, and the exact same result occurs.
            >
            >FYI, line 19 is as follows:
            >
            >$result6 = mysql_fetch_arr ay($query6);[/color]

            It can't have got here and produced the same error, if you put in the error
            handling Kimmo posted.

            Post your revised code for the lines between mysql_query and
            mysql_fetch_arr ay.

            --
            Andy Hassall :: andy@andyh.co.u k :: http://www.andyh.co.uk
            http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool

            Comment

            • briansmccabe@gmail.com

              #7
              Re: displaying the result of a COUNT query in MySQL

              $query6 = "SELECT COUNT (movie_id) as quant FROM movies WHERE divx = 1
              AND format = 'reg'" or die(mysql_error ());
              $result6 = mysql_fetch_arr ay($query6);

              later on the page:

              echo $result6[quant]

              result:

              Warning: mysql_fetch_arr ay(): supplied argument is not a valid MySQL
              result resource in /home/tgupc/public_html/admin/divxreport.php on line
              19


              see for yourself: http://www.tgupc.com/admin/divxreport.php





              Andy Hassall wrote:[color=blue]
              > On 19 Dec 2005 12:01:34 -0800, briansmccabe@gm ail.com wrote:
              > It can't have got here and produced the same error, if you put in the error
              > handling Kimmo posted.
              >
              > Post your revised code for the lines between mysql_query and
              > mysql_fetch_arr ay.[/color]

              Comment

              • Andy Hassall

                #8
                Re: displaying the result of a COUNT query in MySQL

                On 19 Dec 2005 12:24:35 -0800, briansmccabe@gm ail.com wrote:

                Kimmo originally wrote:
                [color=blue][color=green]
                >>$result = mysql_query('SE LECT COUNT(*) AS foo FROM table') or
                >>die(mysql_err or());[/color][/color]

                But you've used:
                [color=blue]
                >$query6 = "SELECT COUNT (movie_id) as quant FROM movies WHERE divx = 1
                >AND format = 'reg'" or die(mysql_error ());[/color]

                You are missing the call to mysql_query().
                [color=blue]
                >$result6 = mysql_fetch_arr ay($query6);[/color]

                ... so $query6 just contains the SQL string, and not a MySQL result set
                resource identifier. You're not actually executing the SQL anywhere, or if you
                are in the rest of the code, you haven't done the error checking there.
                [color=blue]
                >later on the page:
                >
                >echo $result6[quant][/color]

                This should be: $result6['quant'].

                See:



                p.s. You've got the posting style nearly right - however you should put your
                new message _under_ the old one, not above, so the whole message makes sense
                read on its own. You have "top posted".

                --
                Andy Hassall :: andy@andyh.co.u k :: http://www.andyh.co.uk
                http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool

                Comment

                • briansmccabe@gmail.com

                  #9
                  Re: displaying the result of a COUNT query in MySQL


                  Andy Hassall wrote:[color=blue]
                  > On 19 Dec 2005 12:24:35 -0800, briansmccabe@gm ail.com wrote:
                  >
                  > Kimmo originally wrote:
                  >[color=green][color=darkred]
                  > >>$result = mysql_query('SE LECT COUNT(*) AS foo FROM table') or
                  > >>die(mysql_err or());[/color][/color]
                  >
                  > But you've used:
                  >[color=green]
                  > >$query6 = "SELECT COUNT (movie_id) as quant FROM movies WHERE divx = 1
                  > >AND format = 'reg'" or die(mysql_error ());[/color]
                  >
                  > You are missing the call to mysql_query().
                  >[color=green]
                  > >$result6 = mysql_fetch_arr ay($query6);[/color]
                  >
                  > ... so $query6 just contains the SQL string, and not a MySQL result set
                  > resource identifier. You're not actually executing the SQL anywhere, or if you
                  > are in the rest of the code, you haven't done the error checking there.
                  >[color=green]
                  > >later on the page:
                  > >
                  > >echo $result6[quant][/color]
                  >
                  > This should be: $result6['quant'].
                  >
                  > See:
                  >
                  > http://www.php.net/manual/en/languag...es.array.donts
                  >
                  > p.s. You've got the posting style nearly right - however you should put your
                  > new message _under_ the old one, not above, so the whole message makes sense
                  > read on its own. You have "top posted".
                  >
                  > --
                  > Andy Hassall :: andy@andyh.co.u k :: http://www.andyh.co.uk
                  > http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool[/color]

                  Comment

                  • briansmccabe@gmail.com

                    #10
                    Re: displaying the result of a COUNT query in MySQL

                    > ... so $query6 just contains the SQL string, and not a MySQL result set[color=blue]
                    > resource identifier. You're not actually executing the SQL anywhere, or if you
                    > are in the rest of the code, you haven't done the error checking there.[/color]

                    I have this part working now, thanks to your help.
                    [color=blue]
                    >[color=green]
                    > >later on the page:
                    > >
                    > >echo $result6[quant][/color]
                    >
                    > This should be: $result6['quant'].
                    >
                    > See:
                    >
                    > http://www.php.net/manual/en/languag...es.array.donts
                    >[/color]

                    So if I am echoing the result in between strings of HTML, I am having a
                    hard time knowing / remembering how to "escape" the single-quote marks
                    inside the result brackets. Does that make sense?

                    Thanks

                    Brian

                    Comment

                    • briansmccabe@gmail.com

                      #11
                      Re: displaying the result of a COUNT query in MySQL

                      >[color=blue]
                      > ... so $query6 just contains the SQL string, and not a MySQL result set
                      > resource identifier. You're not actually executing the SQL anywhere, or if you
                      > are in the rest of the code, you haven't done the error checking there.[/color]

                      I have this part working now. Thanks for your help.
                      [color=blue][color=green]
                      > >later on the page:
                      > >
                      > >echo $result6[quant][/color]
                      >
                      > This should be: $result6['quant'].
                      >
                      > See:
                      >
                      > http://www.php.net/manual/en/languag...es.array.donts[/color]

                      I have tried to figure out how to single-quote the contents of the []
                      but when it is concatenated in between strings of HTML, it goofs things
                      up. I cannot remember how to do that. I tried a few things I noticed in
                      the page you link to above, but I am not getting very far.

                      Comment

                      • Andy Hassall

                        #12
                        Re: displaying the result of a COUNT query in MySQL

                        On 19 Dec 2005 14:08:15 -0800, briansmccabe@gm ail.com wrote:
                        [color=blue][color=green]
                        >> This should be: $result6['quant'].
                        >>
                        >> See:
                        >>
                        >> http://www.php.net/manual/en/languag...es.array.donts[/color]
                        >
                        > I have tried to figure out how to single-quote the contents of the []
                        >but when it is concatenated in between strings of HTML, it goofs things
                        >up. I cannot remember how to do that. I tried a few things I noticed in
                        >the page you link to above, but I am not getting very far.[/color]

                        Use the "curly brace" format within a string:

                        echo "{$result6['quant']}";




                        --
                        Andy Hassall :: andy@andyh.co.u k :: http://www.andyh.co.uk
                        http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool

                        Comment

                        • william.clarke@gmail.com

                          #13
                          Re: displaying the result of a COUNT query in MySQL

                          briansmcc try your SQL like this...

                          $result6 = mysql_query('SE LECT COUNT (movie_id) as quant FROM movies
                          WHERE divx = 1 AND format = 'reg') or die(mysql_error ());
                          $movie_count = mysql_fetch_arr ay($result6 );

                          later on the page:
                          echo $movie_count['quant'];

                          Comment

                          Working...