beaux1 Posted February 16, 2007 Share Posted February 16, 2007 Right, I'm counting on you PHP gurus! Okay, well, I have a row called items, right, and it has a number in each row, I'd like to count all of the numbers in the 'items' row and echo it. Any ideas? Is this even possible? Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted February 16, 2007 Share Posted February 16, 2007 Use a SUM query... SELECT SUM(item_count) FROM items Quote Link to comment Share on other sites More sharing options...
beaux1 Posted February 16, 2007 Author Share Posted February 16, 2007 lolmathematics Quote Link to comment Share on other sites More sharing options...
renj0806 Posted February 16, 2007 Share Posted February 16, 2007 if you just want to count them and not get the sum of the numbers in each line in the items row. then $result = (result of sql query) $num = mysql_num_rows($result) Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted February 16, 2007 Share Posted February 16, 2007 If you aren't going to be using all of the data that your select query returns, then it is very inefficient to query the database and have it return potentially a lot of data for only a call to mysql_num_rows. If you only want a row count, use the count function in mysql... $result = mysql_query("SELECT COUNT(items) FROM item_table") or die(mysql_error()); $num_of_rows = mysql_result($result, 0); Quote Link to comment Share on other sites More sharing options...
corbin Posted February 16, 2007 Share Posted February 16, 2007 Actually if it's a table of like 99999 items, and you're doing lots of queries, mysql_num_rows could be bad. I would do something like (assumed every row has a column named id): $q = mysql_query("SELECT COUNT('id') AS count FROM items"); $r = mysql_fetch_assoc($q); $number_of_item_rows = $r['count']; Gah someone beat me to it while I was posting Quote Link to comment Share on other sites More sharing options...
beaux1 Posted February 16, 2007 Author Share Posted February 16, 2007 It's a mathematical count, so I have to use SELECT SUM(item_count) FROM items. Items is a row in the information table. I'm really a newb at all of this, so bear with my poor attempt to do this. Items is a row in information. <?php include 'config.php'; include 'opendb.php'; $sql = "SELECT SUM(item_count) FROM information echo $sum['item_count']; include 'closedb.php'; ?> But this isn't going to work, so yeah, what should I do? Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted February 16, 2007 Share Posted February 16, 2007 $sql = "SELECT SUM(item_count) FROM information echo $sum['item_count']; should be $sql = "SELECT SUM(item_count) AS item_count FROM information"; $result = mysql_query($sql); $sum = mysql_fetch_assoc($result); echo $sum['item_count']; or $sql = "SELECT SUM(item_count) FROM information"; $result = mysql_query($sql); echo mysql_result($result, 0); Quote Link to comment Share on other sites More sharing options...
beaux1 Posted February 16, 2007 Author Share Posted February 16, 2007 Ok, problem. I'm actually counting a row called helmet. So I just renamed it, added the or die(mysql_error()); (as I learnt off you in another topic, hitman =P) So my code is: <?php include 'config.php'; include 'opendb.php'; $sql = "SELECT SUM(helmet_count) FROM information"; $result = mysql_query($sql) or die(mysql_error()); echo mysql_result($result, 0); include 'closedb.php'; ?> Unknown column 'helmet_count' in 'field list' The column helmet is there, is there something I'm not picking up here? Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 16, 2007 Share Posted February 16, 2007 as several people said, sum will return all of them added together. count() is what you want. SELECT count(helmet) AS helmet_count Quote Link to comment Share on other sites More sharing options...
beaux1 Posted February 16, 2007 Author Share Posted February 16, 2007 See that works: <?php include 'config.php'; include 'opendb.php'; $sql = "SELECT count(helmet) AS helmet_count FROM information"; $result = mysql_query($sql) or die(mysql_error()); echo mysql_result($result, 0); include 'closedb.php'; ?> Except, it's not what I want. That is counting the number of rows, I want to count the integers in every line under the helmet row. So like: |helmet| 2 3 1 PHP Script will count them and output would be = 6 Quote Link to comment Share on other sites More sharing options...
beaux1 Posted February 16, 2007 Author Share Posted February 16, 2007 Wewt, works, I just replaced SELECT count(helmet with SELECT sum(helmet. <?php include 'config.php'; include 'opendb.php'; $sql = "SELECT sum(helmet) AS helmet_count FROM information"; $result = mysql_query($sql) or die(mysql_error()); echo mysql_result($result, 0); include 'closedb.php'; ?> Now, anyone know how I would go about calculating multiple rows? Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted February 16, 2007 Share Posted February 16, 2007 Now, anyone know how I would go about calculating multiple rows? What do you mean? Quote Link to comment Share on other sites More sharing options...
beaux1 Posted February 16, 2007 Author Share Posted February 16, 2007 So the script does the same thing, except count lets say, helmet, shoe, sock and more rows I choose. All other rows basically, the result wouldn't be them added up alltogether, it would just echo the sum for each row. Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted February 16, 2007 Share Posted February 16, 2007 Same way, just separate each column by a comma... $query = "SELECT SUM(helmet) AS helmet_count, SUM(shoe) AS shoe_count, SUM(sock) AS sock_count FROM information"; $result = mysql_query($query) or die(mysql_error()); $helmet_count = mysql_result($result, 0, "helmet_count"); $shoe_count = mysql_result($result, 0, "shoe_count"); $sock_count = mysql_result($result, 0, "sock_count"); Quote Link to comment Share on other sites More sharing options...
beaux1 Posted February 16, 2007 Author Share Posted February 16, 2007 <?php include 'config.php'; include 'opendb.php'; $query = "SELECT SUM(helmet) AS helmet_count, SUM(belt) AS belt_count, SUM(earplugs) AS earplugs_count FROM information"; $result = mysql_query($query) or die(mysql_error()); $helmet_count = mysql_result($result, 0, "helmet_count"); $belt_count = mysql_result($result, 0, "belt_count"); $earplugs_count = mysql_result($result, 0, "earplugs_count"); echo mysql_result($result, 0); include 'closedb.php'; ?> It's only echo-ing helmet_count, though. Quote Link to comment Share on other sites More sharing options...
beaux1 Posted February 16, 2007 Author Share Posted February 16, 2007 Bump for good justice. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.