#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    2
    Rep Power
    0

    Error msg: "Resource id #22" regarding COUNT(*)


    Hi,
    First post on this forum

    I am trying to use the simple query:

    PHP Code:
    $sql mysql_query("SELECT COUNT(*) FROM table WHERE active = '1'");
    echo 
    "$sql"
    however, instead of getting a count I get the message: "Resource id #22".

    It is in fact the first time i use this function within MySQL, but I think this is a PHP thingie...

    Funnily enough, you put "Resource id #22" in Google and not many sites come up, and non with a clear response as to what it is.

    Any ideas?

    Thanks.

    PeterG
  2. #2
  3. Retired
    Devshed Supreme Being (6500+ posts)

    Join Date
    Feb 2002
    Location
    Finland
    Posts
    9,143
    Rep Power
    2493
    PHP Code:
    $sql mysql_fetch_row(mysql_query("SELECT COUNT(*) FROM table WHERE active = '1'"));
    echo 
    $sql[0]; 
    Would be quicker doing count (id) or something similar.

    HTH

    Cheers,
    Jamie

    >_ skiFFie ? | Twitter

    __________________

    Let the might of your compassion arise to bring a quick end
    to the flowing stream of the blood and tears .....
    Please hear my anguished words of truth.

    __________________
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    2
    Rep Power
    0
    whatI'm actually trying to do is a pagination break.... counting how many rows there are and divide them accoring to the number of elements i want per page...

    I've seen several scripts but want to do it my way...

    is the LIMIT attribute in the SQL command also a BETWEEN type?? such as "limit 1, 10": would it limit between these to number of rows??
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2011
    Posts
    1
    Rep Power
    0
    I had a similar issue with Resource id #22.... I found out what I was doing wrong, maybe this will help anyone having a similar problem.

    I was using
    Code:
    <?=
    instead of
    Code:
    <?php
    Check that and make sure it is correct.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    140
    Rep Power
    14
    In your case, $sql is not the result of the query as you seem to expect. It's a pointer to the parsed query. You still have to fetch the rows using mysql_fetch_row.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    170
    Rep Power
    55
    When using php, an SQL query always returns an array. You're attempting to echo an array without identifying what you want to do with the array. Also, as jabba_29 said, don't select results that you don't need. In this query, you're only looking for results where "active" is equal to "1", so only look for results in the "active" column and ignore everything else.
    PHP Code:
    //method 1 - Count in the SQL query
    $sql mysql_query("SELECT COUNT(active) FROM table WHERE active='1'");
    $total mysql_fetch_array($sql);
    echo 
    $total[0];

    //method 2 - Count the results of the query
    $sql mysql_query("SELECT active FROM table WHERE active='1'");
    $total mysql_num_rows($sql);
    echo 
    $total[0]; 
    Supposedly, SQL is faster than PHP, but in this kind of search you would only notice a difference if you were looking at millions (maybe hundreds of millions) of results.
  12. #7
  13. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6352
    This thread is 7 years old. I'm closing it.

    The resource ID problem is discussed in the new user guide (which didn't exist in 2004)
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo