Go Back   Webmaster Malaysia Forum » Website Design & Development » Website Programming

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 19-01-2004, 03:53 PM
jeb23's Avatar
New kid on the block
 
Join Date: Jan 2004
Location: JB
Posts: 4
Rep Power: 0
jeb23 is on a distinguished road
Count total of records

salam sejahtera..
saya bdak baru blajar php...
kalau tak keberatan leh tumpang tanya tak?
ape care/scripts/function yg paling senang tuk 'sum'kn jumlah rekod dlm db (mysql - dlm satu table.)?
thanks...
regards
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 19-01-2004, 04:07 PM
Senior Webmaster
 
Join Date: Oct 2001
Location: Melbourne, AU
Posts: 456
Rep Power: 95
sufyan is on a distinguished road
PHP Code:
$db //db connection

$sql mysql_query("SELECT * FROM table"$db);
$rows mysql_num_rows($sql);

//echo $rows; 
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 19-01-2004, 04:35 PM
jeb23's Avatar
New kid on the block
 
Join Date: Jan 2004
Location: JB
Posts: 4
Rep Power: 0
jeb23 is on a distinguished road
thanks.. but
PHP:--------------------------------------------------------------------------------
$db = //db connection

$sql = mysql_query("SELECT * FROM table", $db);
$rows = mysql_num_rows($sql);

//echo $rows;

perhaps it works.....

mysql_num_rows kite bleh dapatkan jumlah rekod atau bil rekod yg affect ngan query?

apa beza ngan "select count(nama medan) from nama table";
thanks so much!!!

refards
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 19-01-2004, 04:56 PM
Senior Webmaster
 
Join Date: Oct 2001
Location: Melbourne, AU
Posts: 456
Rep Power: 95
sufyan is on a distinguished road
PHP Code:
$sql mysql_query("SELECT COUNT(Key) FROM table");
$rows mysql_result($sql$db);

//echo $rows; 
There isn't much difference between that example and the one earlier if your not running a large database.

The performance factor between the two methods is negligible for a small database.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 19-01-2004, 05:46 PM
jeb23's Avatar
New kid on the block
 
Join Date: Jan 2004
Location: JB
Posts: 4
Rep Power: 0
jeb23 is on a distinguished road
maybe my question does not so clear... what is really i want to ask is there any easiest way to 'sum' (count the sum) of your records in the db?
let say that
user vol
abu 5
abu 8
abu 9
ali 9

is a record on db.....
>> the sum of vol for abu is 22 right?
so... how can i get these '22'?

thanks.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 19-01-2004, 05:56 PM
Senior Webmaster
 
Join Date: Oct 2001
Location: Melbourne, AU
Posts: 456
Rep Power: 95
sufyan is on a distinguished road
Ic,

for this example we will have 2 fields:
Name (abu, ali, etc.) and
Number (5,9, etc.)

The easiest way is to do something like the following:

PHP Code:
$final 0;

$db = ... ;

$sql mysql_query("SELECT * FROM table WHERE Name='abu'");
while (
$output mysql_fetch_array($sql)) {
    
$final $final $output['Number'];
}

echo 
$final
I haven't tested the code, but i'm quite sure it should work.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 20-01-2004, 10:17 AM
Senior Webmaster
 
Join Date: Oct 2001
Location: Kuala Lumpur, Malaysia
Posts: 292
Rep Power: 91
kidino is on a distinguished road
Send a message via Yahoo to kidino
Use this SQL
Code:
$sql = "select sum(vol) where user like 'abu'";
Why ... ?? Whenever possible, put the data calculation at the db server. Great advantage especially when webserver & db server are two different machine. Second, when dealing with text or string, use LIKE rather than "=".
__________________
--------------------------------------------
Khairil Iszuddin Ismail

Last edited by kidino; 20-01-2004 at 10:39 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 20-01-2004, 05:34 PM
white_neck's Avatar
Senior Webmaster
 
Join Date: Jan 2003
Location: dreamland
Posts: 240
Rep Power: 75
white_neck is on a distinguished road
Send a message via Yahoo to white_neck
btul tuh..
better buat kat sql query.. then buat count on looping...
jeb23 try ler sql query tuh...
mesti jadik nyer..
hahahahha...
__________________
^_^ <== stay happy
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 21-01-2004, 12:10 PM
jeb23's Avatar
New kid on the block
 
Join Date: Jan 2004
Location: JB
Posts: 4
Rep Power: 0
jeb23 is on a distinguished road
mr sufyan, kidino......
saya dah try dua2.. both code dapat result yg sama.....
thanks so much!!!!!!!!!!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 21-01-2004, 04:17 PM
white_neck's Avatar
Senior Webmaster
 
Join Date: Jan 2003
Location: dreamland
Posts: 240
Rep Power: 75
white_neck is on a distinguished road
Send a message via Yahoo to white_neck
ini pandangan aku ler... <== korang boleh celah kalau aku silap

result memang sama, tp lebih ekfisien (btul ke eja ni ) kalau guna terus kat query. sbb nak elak looping while tuh... so aku rasa lg cepat guna query tuh... cepat skit ler.. bukan byk pun.. hihihik

btul tak? ker salah?
__________________
^_^ <== stay happy
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #11 (permalink)  
Old 21-01-2004, 06:13 PM
Senior Webmaster
 
Join Date: Oct 2001
Location: Melbourne, AU
Posts: 456
Rep Power: 95
sufyan is on a distinguished road
That is correct. Using the direct query method does eliminate a process and does improve efficiency. However, the improvement in performance between the two methods are negligible for a small database.

The example given in this topic is very basic. If you needed a more complex function, the second method would be of more use as you could intergrate the power of PHP to manipulate your data.

As for 'LIKE' rather than "=", I disagree with that statement as the equal sign (=) is the equivalent of IN in SQL, which returns exact matches.

LIKE is better used for a pattern with wildcards (%).

Anyhow, both methods will work ('IN' - = or 'LIKE' - without wildcards %), and there are no performance differences between the two.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #12 (permalink)  
Old 24-01-2004, 06:58 PM
hymns's Avatar
Senior Webmaster
 
Join Date: Nov 2001
Location: Johor
Posts: 768
Rep Power: 100
hymns is on a distinguished road
Send a message via ICQ to hymns Send a message via Yahoo to hymns
ya ya! walaupun dah pakai zend optimize =)
__________________
I hate when:

vBulletin Message:
Sorry! The administrator has specified that users can only post one message every 60 seconds
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #13 (permalink)  
Old 26-01-2004, 02:44 PM
white_neck's Avatar
Senior Webmaster
 
Join Date: Jan 2003
Location: dreamland
Posts: 240
Rep Power: 75
white_neck is on a distinguished road
Send a message via Yahoo to white_neck
Quote:
Originally posted by hymns
ya ya! walaupun dah pakai zend optimize =)
aku tak ler hustler sgt..
so aku nak tanya ler..
zend optimize tu buat aper?
aper function dier dlm php?
aku dah baca skit review dier.
tp tak paham
__________________
^_^ <== stay happy
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #14 (permalink)  
Old 27-01-2004, 05:45 PM
hymns's Avatar
Senior Webmaster
 
Join Date: Nov 2001
Location: Johor
Posts: 768
Rep Power: 100
hymns is on a distinguished road
Send a message via ICQ to hymns Send a message via Yahoo to hymns
aler tambah performance bg yg run server lah.. tapi kalau small / developer x rasa beza pong...
__________________
I hate when:

vBulletin Message:
Sorry! The administrator has specified that users can only post one message every 60 seconds
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #15 (permalink)  
Old 28-01-2004, 09:01 AM
white_neck's Avatar
Senior Webmaster
 
Join Date: Jan 2003
Location: dreamland
Posts: 240
Rep Power: 75
white_neck is on a distinguished road
Send a message via Yahoo to white_neck
Ooo..
benda tuh kena install kat server ker..
aku ingat dier cam ader panggil some other component..

ok tq
__________________
^_^ <== stay happy
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Visitor's Count patcjh Website Programming 12 24-01-2007 05:22 AM
Total newbie here >_< Pip_X Website Programming 2 12-04-2006 01:12 PM
Total form looping textfield koisempoi Website Design 2 09-05-2005 12:35 PM
Whois: total domain hosted Filuren Paid Hosting Discussion Forum 12 08-10-2004 01:54 AM
PHP FrameWork For TOTAL NEWBIE bazet Website Programming 13 02-12-2002 04:19 PM



All times are GMT +8. The time now is 03:15 PM. Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO 3.1.0 vBulletin skin by ForumMonkeys.com.


WebmasterMalaysia.com is Proudly Hosted by Exabytes Semi Dedicated Server.