SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: COUNT question

  1. #1
    SitePoint Enthusiast MarkW78's Avatar
    Join Date
    Mar 2001
    Location
    Wellington, Ohio
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    COUNT question

    Hi everyone. This is probably another dumb question, but I'm still learning so please bear with me for a bit.

    I am building a small photo gallery and have things so that users can search through several different fields in each record. But I am having problems counting the number of photos that match the search criteria. I have been successful in incrementing a variable in a WHILE loop, but that doesn't do exactly what I'd like, because it only counts what is displayed then and if I'm limiting the results to 10 per screen, it will normally say 10.

    So how do I get it to go through the entire table and count the matching records while still limiting the results to 10 per page? Here is the code that I have, but it isn't working right:

    <?
    $counter = mysql_query("SELECT COUNT(*) FROM gallery WHERE Picture_Keywords LIKE \"%$find%\" ");
    ?>

    The rest of my code to show the pictures works fine, but when I set $counter to print to the screen, it prints the following:

    Resource id #2

    I'm curious to know what that means and how to fix this. I'd really appreciate any help you can give. Thanks in advance!
    Mark Wright
    Wrightsites.com - Better Hosting. Better Price.

  2. #2
    Victory shall be mine tubedogg's Avatar
    Join Date
    Mar 2001
    Location
    Medina, OH
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The problem is you are forgetting the fundamental thing about queries - they return the results in a result identifier and you need to pull the results you want from there. (That's what the Resource message is about.) Also you need to name the column in the result. Here's a bit to get you going:

    PHP Code:
    $counter mysql_query("SELECT COUNT(*) AS totalresults FROM gallery WHERE Picture_Keywords LIKE \"%$find%\"");
    $total mysql_result($counter,0,0); 
    mysql_result returns one cell or field of a result set.

    mysql_result (resource result, int row [, mixed field])
    The first item is the variable you assigned the query to, the second is the row offset (if any, 0 being the first row) from the top of the result set, and the third can be either the column offset (again 0 being the first column) or the name of the column (or in our case the alias we assigned to it - totalresults).
    Last edited by tubedogg; Jun 9, 2001 at 15:09.
    Kevin

  3. #3
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    The previous post has some errors. Queries do not return arrays. They return what is called a "result identifier". You need to do some further work to extract the data you queried with other mysql functions. Those functions return arrays or objects depending on which ones you use.
    Last edited by 7stud; Jun 8, 2001 at 23:55.

  4. #4
    Victory shall be mine tubedogg's Avatar
    Join Date
    Mar 2001
    Location
    Medina, OH
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    He's right about the array part but the rest of the message is valid and should solve your problem.
    Kevin


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •