jonnyfortis Posted July 8, 2013 Share Posted July 8, 2013 i have a table (cfsCustomersImages) containing the columns imagesID image custID imageWant the value of column imageWant is default "N" if the user wants and image they have a form they can check to change the value to "Y" what i need to show is a value of the total of "Y" values in the column for example if 6 "Y"'s are in the columns 6 images have been selected does this make sense? Quote Link to comment https://forums.phpfreaks.com/topic/279960-show-a-total-based-on-a-column-value/ Share on other sites More sharing options...
thara Posted July 8, 2013 Share Posted July 8, 2013 (edited) In this case you need to use MySQL COUNT function (I assume your RDMS is mysql). The COUNT function returns the number of rows in a table. The COUNT function allows you to count all rows in a table or rows that match a particular condition. So now your query should be something like this. SELECT COUNT(*) FROM cfsCustomersImages WHERE imageWant = 'Y' What you are looking for, THIS IS SQL FIDDLE Edited July 8, 2013 by thara Quote Link to comment https://forums.phpfreaks.com/topic/279960-show-a-total-based-on-a-column-value/#findComment-1439905 Share on other sites More sharing options...
jonnyfortis Posted July 8, 2013 Author Share Posted July 8, 2013 (edited) hi, thanks for you response i have tried your suggestion but and not getting the results. i have put in my workings $colname_rsCustomer = "-1"; if (isset($_SESSION['MM_Username'])) { $colname_rsCustomer = $_SESSION['MM_Username']; } mysql_select_db($database_cfs, $cfs); $query_rsCustomer = sprintf("SELECT * FROM cfsCustomers, cfsCustomersImages WHERE custEmail = %s AND cfsCustomersImages.custID = cfsCustomers.custID", GetSQLValueString($colname_rsCustomer, "text")); $query_limit_rsCustomer = sprintf("%s LIMIT %d, %d", $query_rsCustomer, $startRow_rsCustomer, $maxRows_rsCustomer); $rsCustomer = mysql_query($query_limit_rsCustomer, $cfs) or die(mysql_error()); $row_rsCustomer = mysql_fetch_assoc($rsCustomer); mysql_select_db($database_cfs, $cfs); $query_rsCount = sprintf("SELECT COUNT(*) FROM cfsCustomers, cfsCustomersImages WHERE cfsCustomers.custID = cfsCustomersImages.custID AND custEmail = %s AND imageWANT = 'Y'", GetSQLValueString($colname_rsCustomer, "text")); $rsCount = mysql_query($query_rsCount, $cfs) or die(mysql_error()); $row_rsCount = mysql_fetch_assoc($rsCount); $totalRows_rsCount = mysql_num_rows($rsCount); and echoing out the results <?php echo $row_rsCount['imageWant']; ?> basically i was trying to get the results based on the logged in user Edited July 8, 2013 by jonnyfortis Quote Link to comment https://forums.phpfreaks.com/topic/279960-show-a-total-based-on-a-column-value/#findComment-1439914 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.