January 7th, 2004, 07:36 AM
Error msg: "Resource id #22" regarding COUNT(*)
First post on this forum
I am trying to use the simple query:
however, instead of getting a count I get the message: "Resource id #22".
$sql = mysql_query("SELECT COUNT(*) FROM table WHERE active = '1'");
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.
January 7th, 2004, 07:42 AM
Would be quicker doing count (id) or something similar.
$sql = mysql_fetch_row(mysql_query("SELECT COUNT(*) FROM table WHERE active = '1'"));
>_ 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.
January 11th, 2004, 12:03 AM
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??
November 3rd, 2011, 03:42 PM
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 instead of Check that and make sure it is correct.
November 3rd, 2011, 04:03 PM
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.
November 3rd, 2011, 04:13 PM
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.
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.
//method 1 - Count in the SQL query
$sql = mysql_query("SELECT COUNT(active) FROM table WHERE active='1'");
$total = mysql_fetch_array($sql);
//method 2 - Count the results of the query
$sql = mysql_query("SELECT active FROM table WHERE active='1'");
$total = mysql_num_rows($sql);
November 3rd, 2011, 06:18 PM
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.