/    Sign up×
Community /Pin to ProfileBookmark

How To Count All Records..

I HAVE A PHP SCRIPT THAT COUNTS ALL RECORDS IN MY TABLE..I CANT GET IT TO WORK…I KNOW IT MIGHT BE SIMPLE BUT IM LOST..HERE IS THE SCRIPT

[PHP
//DATABASE CONNECTION STUF
$connect=mysql_connect ($mysql_server, $mysql_user, $mysql_password) or die(“cannot make connection”);
$dbselect=mysql_select_db (showcase) or die(“cannot select database”);
$count = mysql_query(“SELECT COUNT(*) FROM `gd-gallery`;”);
//$row=mysql_fetch_row($count);
echo $count;
//select count(*
),date from records group by date
?>
[/code]

THIS RETURNS ‘Resource id #2’ ON THE BROWSER…URRR.

to post a comment
PHP

10 Comments(s)

Copy linkTweet thisAlerts:
@artemisSep 23.2004 — [font=technical][size=3][font=monospace][size=5]

[code=php]
$numrows=myswl_numrows($count);
[/code]
[/size][/font]

You had not counted how many rows there are.[/size][/font]
Copy linkTweet thisAlerts:
@HellspireSep 23.2004 — [i]Originally posted by vicpal25 [/i]
[code=php]
//DATABASE CONNECTION STUF
$connect=mysql_connect ($mysql_server, $mysql_user, $mysql_password) or die("cannot make connection");
$dbselect=mysql_select_db (showcase) or die("cannot select database");
$count = mysql_query("SELECT COUNT(*) FROM gd-gallery;");
//$row=mysql_fetch_row($count);
echo $count;
//select count(*),date from records group by date
?>
[/code]

THIS RETURNS 'Resource id #2' ON THE BROWSER...URRR.[/QUOTE]



Um..... 'ewwwwwwww...........'

There are some problems you should note....

[code=php]
$connect = mysql_connect($host,$user,$pass);
$dbselect = mysql_select_db($db,$connect);
/*
You did not include the second parameter int he select_db and while this will work in some versions of PHP, it wont work in others... please correct, also you had not entered a $ in front of showcase so I dont know what the hell that is....
*/

/*
ASC is ascending, and DESC is descending....
I do not know whether you are a fool *im sorry* or your trying to do somethign more complicated than it appears but.. here goes

If you are trying just to count the rows... below will work...
*/
$query = "SELECT * FROM tablename ORDER BY date ASC";
$count = mysql_num_rows(mysql_query($query));
echo $count;
/*
If you want to do somethign like only get certain rows or whatever then thats that... with the WHERE clause of queries... anything more complicated than that will require for and while functions or splitting things into arrays.... let me know if you need a more complicated approach ....
*/
[/code]
Copy linkTweet thisAlerts:
@NogDogSep 23.2004 — [code=php]
$result = mysql_query("SELECT COUNT(*) FROM gd-gallery");
if($result)
{
$row = mysql_fetch_row($result);
echo "<p>Count = $row[0]</p>n";
}
else
{
echo "<p>Query failed.</p>n";
}
[/code]
Copy linkTweet thisAlerts:
@HellspireSep 23.2004 — [i]Originally posted by NogDog [/i]

[B][code=php]
$result = mysql_query("SELECT COUNT(*) FROM gd-gallery");
if($result)
{
$row = mysql_fetch_row($result);
echo "<p>Count = $row[0]</p>n";
}
else
{
echo "<p>Query failed.</p>n";
}
[/code]
[/B][/QUOTE]


*shrugs* to each his own method..... I hardly use plain sql I have it classed (by myself) so I can do pretty much anything in different ways/... but the original way (i have shown it above) IS SOOOOOO MUCH SIMPLER

E; Dont forget to read my former post, why your code could be flawed (first comment)
Copy linkTweet thisAlerts:
@vicpal25authorSep 23.2004 — both methods work! niceeeeeeeeeeeee! thanks!
Copy linkTweet thisAlerts:
@NogDogSep 23.2004 — [i]Originally posted by Hellspire [/i]

[B]*shrugs* to each his own method..... I hardly use plain sql I have it classed (by myself) so I can do pretty much anything in different ways/... but the original way (i have shown it above) IS SOOOOOO MUCH SIMPLER



E; Dont forget to read my former post, why your code could be flawed (first comment) [/B]
[/QUOTE]

I was essentially just taking the original poster's code and showing where the mistake was: reading the result of the mysql_query instead of reading the actual value returned by the count(*) in the query itself. Your method will return the same value, and if it's easier for you to read, I have no problem with that. However, I would think that if you were dealing with a database table with several thousand rows and many columns, using a count(*) query would likely be more efficient in terms of response time and memory usage (all the count(*) query has to return is one row with 1 column, instead of every row with every column). But as the Perl users like to say, "TIMTOWTDI" (there is more than on way to do it).
Copy linkTweet thisAlerts:
@pyroSep 23.2004 — Also, just to point out...

If you are simply trying to return the number of rows, there is no reason to select everything (SELECT *) or to order it in any way (ORDER BY date ASC). Using the method that Hellspire posted, I'd think this would be a bit more efficient:

[code=php]$query = "SELECT column_name FROM tablename;";
$count = mysql_num_rows(mysql_query($query));
echo $count;[/code]
Copy linkTweet thisAlerts:
@NogDogSep 23.2004 — [i]Originally posted by pyro [/i]

[B]Also, just to point out...



If you are simply trying to return the number of rows, there is no reason to select everything (SELECT *) or to order it in any way (ORDER BY date ASC). Using the method that Hellspire posted, I'd think this would be a bit more efficient:



[code=php]$query = "SELECT column_name FROM tablename;";
$count = mysql_num_rows(mysql_query($query));
echo $count;[/code]
[/B][/QUOTE]

But, if there are 50,000 rows in table "tablenme", you would still be returning all 50,000 1-column result rows to PHP for the $count variable to point to, instead of one 1-column row with a count(*) query. Again, not a big deal 99.9% of the time for most of us, I expect; but if you are working with a very large database it might be a concern. Of course, with the powerful computers we have today and cheap memory, we seldom have to worry too much about such things - at least until they gang up on us when we least expect it and bite us in the butt. ?
Copy linkTweet thisAlerts:
@pyroSep 23.2004 — I wasn't arguing with you; I was simply pointing out that returning all columns and ordering the rows in any way simply to get a count makes no sense. You are probably [i]usually[/i] going to be better off using an SQL statement to do what you want, rather than manipulating the data with PHP.
Copy linkTweet thisAlerts:
@NogDogSep 23.2004 — Awww... come on, argue with me! ?

Anyway, I thought I was just discussing it. Not being much more than a PHP hack (like I am with most things), I was just expressing my doubts (and I guess hoping someone would confirm them or show me the errors in my thinking).

Nah, that's not really it. I was just finding something to do other than concentrate on the boring work I should be concentrating on. :rolleyes:
×

Success!

Help @vicpal25 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...