Current location: Hot Scripts Forums » Programming Languages » PHP » Counting rows from a table ...


Counting rows from a table ...

Reply
  #1 (permalink)  
Old 10-12-03, 08:13 AM
Seldimi Seldimi is offline
Newbie Coder
 
Join Date: Sep 2003
Location: Kozani, Greece
Posts: 50
Thanks: 0
Thanked 0 Times in 0 Posts
Counting rows from a table ...

Hello, I am searching for a way to count rows from more than one table in my database.
I use for counting one table :
Code:
$sql = "SELECT * FROM wplanet_freestuff";
$result = mysql_query($sql);
$num = mysql_num_rows($result);
echo "$num";
What to use for counting 10 tables ???
Please Help
Reply With Quote
  #2 (permalink)  
Old 10-13-03, 02:02 AM
afkaar afkaar is offline
Newbie Coder
 
Join Date: Aug 2003
Location: UaE
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
for me, idon't know function that count from or than a table. but in this case i'll do the folwing:
make an array of ur tables list :
Code:
$tb[1]="table1";
$tb[2]="table2";
$tb[3]="table3";
$tb[4]="table4";
for($i=1;$i<=4;$i++){
$sql = "SELECT * FROM $tb[$i]";
$result = mysql_query($sql);
$num[$i] = mysql_num_rows($result);
$nm+=$num[$i] ;
}
echo"$nm";
Reply With Quote
  #3 (permalink)  
Old 10-13-03, 05:38 AM
ermau's Avatar
ermau ermau is offline
Wannabe Coder
 
Join Date: Aug 2003
Location: Florida, USA
Posts: 240
Thanks: 0
Thanked 0 Times in 0 Posts
Why put a variable in quotes? This code is much more effecient:

PHP Code:

$tb = array(1   => 'table1',

            
2   => 'table2',
            
3   => 'table3',
            
4   => 'table4');
$rowTotals = array();

for (
$i=1$i<count($tb); $i++)
{
     
$request mysql_query('SELECT COUNT(*) FROM '.$tb[$i]);
     
$rows     mysql_fetch_row($request);
     
mysql_free_result($request);
     
$rowsTotal["$tb[$i]"] = $rows[0];

That will give you the ammount of rows for each individual array. It's much more effecient to do the query for the count, rather than query everything. $rowsTotal contains every table's rows, the key is the table name. The for loop is also dynamic so that if you add any to the array it will compensate automatically.
__________________
PHP / mySQL Developer
Reply With Quote
  #4 (permalink)  
Old 10-13-03, 07:29 AM
alesh.ru alesh.ru is offline
Internet-paraSITE
 
Join Date: Jul 2003
Location: Moscow, Russia
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
PHP Code:

list($all[$i]) = mysql_fetch_row(mysql_query("select count(*) from $table")); 

it's faster
__________________
From Russia with love
Reply With Quote
  #5 (permalink)  
Old 10-13-03, 01:25 PM
ermau's Avatar
ermau ermau is offline
Wannabe Coder
 
Join Date: Aug 2003
Location: Florida, USA
Posts: 240
Thanks: 0
Thanked 0 Times in 0 Posts
It's not really all that different, and you still have to run it through a loop. It's also more confusing to the newbs and just harder to read in general, that's why I didn't do something like that.
__________________
PHP / mySQL Developer
Reply With Quote
  #6 (permalink)  
Old 10-14-03, 02:48 PM
Seldimi Seldimi is offline
Newbie Coder
 
Join Date: Sep 2003
Location: Kozani, Greece
Posts: 50
Thanks: 0
Thanked 0 Times in 0 Posts
Thank you
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Validator ctrl in table cell : Strange behaviour sunny_doncaster ASP.NET 6 07-31-08 09:43 AM
how to validate mutiple rows using the same input text Han84 JavaScript 1 08-02-07 09:14 AM
Auto-emailing on table insert mdhall PHP 5 10-13-03 10:14 AM
Autonum in mysql table pk_jsp PHP 1 10-11-03 04:32 PM
moving data from table to table ..please help! geneane ASP 2 09-22-03 07:02 PM


All times are GMT -5. The time now is 07:06 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.