Angeleyezz Posted October 6, 2012 Share Posted October 6, 2012 for some reason its not showing up how it should. if there is a row of data it shows up, if it is empty, it shows nothing, not the 0.00 that its supposed to. any ideas? <?php $q1_cycle_oil = "SELECT SUM(amount) AS q1_cycle_oil_exp FROM expense_fuel WHERE fuel_code = 017 AND year = $exp_year AND month between 01 and 03"; $q1_cycle_oil_results = mysql_query($q1_cycle_oil) or die(mysql_error()); if (mysql_num_rows($q1_cycle_oil_results) < 1 ) { echo "0.00"; } else { while($row = mysql_fetch_array($q1_cycle_oil_results)) { echo $row['q1_cycle_oil_exp']; } } ?> Quote Link to comment Share on other sites More sharing options...
tgavin Posted October 6, 2012 Share Posted October 6, 2012 What does this produce? $count = mysql_num_rows($q1_cycle_oil_results); echo $count; Quote Link to comment Share on other sites More sharing options...
Angeleyezz Posted October 6, 2012 Author Share Posted October 6, 2012 it should produce 1 row of data, so the return should be true and it should post it. witch it does, but if there are no entries its not posting anything, not even my 0.00 entry return Quote Link to comment Share on other sites More sharing options...
Angeleyezz Posted October 6, 2012 Author Share Posted October 6, 2012 it does, it produces 1 Quote Link to comment Share on other sites More sharing options...
tgavin Posted October 6, 2012 Share Posted October 6, 2012 Check your query. Test it in phpmyadmin and make sure you're getting the results you want first. then bring it back into your script. Quote Link to comment Share on other sites More sharing options...
Angeleyezz Posted October 6, 2012 Author Share Posted October 6, 2012 the query is ok, if theres a result its printing the result the way it should be, but if its empty its not printing the 0.00 in the else statment, i just switched it around a bit and its still not working <?php $q2_cycle_oil = "SELECT SUM(amount) AS q2_cycle_oil_exp FROM expense_fuel WHERE fuel_code = 017 AND year = $exp_year AND month between 04 and 06"; $q2_cycle_oil_results = mysql_query($q2_cycle_oil) or die(mysql_error()); $q2_cycle_oil_count = mysql_num_rows($q2_cycle_oil_results); if ($q2_cycle_oil_count >= 1) { while($row = mysql_fetch_array($q2_cycle_oil_results)) { echo $row['q2_cycle_oil_exp']; } } else { echo "0.00"; } ?> Quote Link to comment Share on other sites More sharing options...
tgavin Posted October 6, 2012 Share Posted October 6, 2012 apparently it's not empty. what do you get when you run the query in phpmyadmin? Quote Link to comment Share on other sites More sharing options...
Angeleyezz Posted October 6, 2012 Author Share Posted October 6, 2012 thats the thing, i have this on 2 tables, 1 for quarter 1 and 1 for quarter 2, the quarter 1 is not empty it has 1 record, and the quarter 2 is empty and has no records. its printing the value fine if i take the conditional statement out and just run it raw if theres a result print it, if not print nothing lol. but when i add the conditionals "if else" its printing the record if there is 1, if theres no record its not printing anything when it should print 0.00 Quote Link to comment Share on other sites More sharing options...
tgavin Posted October 6, 2012 Share Posted October 6, 2012 (edited) I must not be understanding something, because it looks to me it's doing exactly what you're telling it to do. Try this and see if anything changes if($q2_cycle_oil_count > 0) instead of if ($q2_cycle_oil_count >= 1) Edited October 6, 2012 by tgavin Quote Link to comment Share on other sites More sharing options...
Angeleyezz Posted October 6, 2012 Author Share Posted October 6, 2012 Nothing changed. if theres a record its printing the record, if theres no record its blank and not printing the 0.00 =( Quote Link to comment Share on other sites More sharing options...
tgavin Posted October 6, 2012 Share Posted October 6, 2012 I know you say the queries are fine, but did you actually check them in phpmyadmin? Quote Link to comment Share on other sites More sharing options...
Jessica Posted October 6, 2012 Share Posted October 6, 2012 Echo the query with the values filled in and run it in MySQL. By using a sum you may just be getting a sum of 0. Quote Link to comment Share on other sites More sharing options...
Angeleyezz Posted October 6, 2012 Author Share Posted October 6, 2012 (edited) I don't know how to check them in phpmyadmin, never did that before. i think jessica is right, im using a sum to count the values of a specific field, but does it really matter becuase all my condition is asking is that if it finds anything to print the sum, if it doesnt find anything just print 0.00 im confused =( Edited October 6, 2012 by Angeleyezz Quote Link to comment Share on other sites More sharing options...
Jessica Posted October 6, 2012 Share Posted October 6, 2012 Echo the query. Then copy and paste that into phpmyadmin SQL window. Quote Link to comment Share on other sites More sharing options...
Angeleyezz Posted October 6, 2012 Author Share Posted October 6, 2012 Jessica you were right, it was because i was calling a sum. i added a regular query first to check to see if it was there, then in my conditional i used the sum query and the else as the 0.00 <!-- Q1 CYCLE OIL BREAKDOWN --> <td width="25%" valign="top"><font face="verdana" size="1"><b>Q1 Cycle Oil</b><br />$ <?php $q1_cycle_oil_check = "SELECT * FROM expense_fuel WHERE fuel_code = 017 AND year = $exp_year AND month between 01 and 03"; $q1_cycle_oil_check_results = mysql_query($q1_cycle_oil_check) or die(mysql_error()); $q1_cycle_oil_check_count = mysql_num_rows($q1_cycle_oil_check_results); if ($q1_cycle_oil_check_count > 0) { $q1_cycle_oil = "SELECT SUM(amount) AS q1_cycle_oil_exp FROM expense_fuel WHERE fuel_code = 017 AND year = $exp_year AND month between 01 and 03"; $q1_cycle_oil_results = mysql_query($q1_cycle_oil) or die(mysql_error()); while($row = mysql_fetch_array($q1_cycle_oil_results)) { echo $row['q1_cycle_oil_exp']; } } else { echo "0.00"; } ?> </font></td> <!-- / END / Q1 CYCLE OIL BREAKDOWN --> thank you for the help guys =) Quote Link to comment Share on other sites More sharing options...
Jessica Posted October 6, 2012 Share Posted October 6, 2012 That makes no sense. Why don't you just echo the sum no matter what it is??? Quote Link to comment Share on other sites More sharing options...
Angeleyezz Posted October 6, 2012 Author Share Posted October 6, 2012 (edited) like the way i had it origionally? im not sure how to do it like you mean. i dont like the way i have it either lol an extra query for nothing lol. can you show me what you mean jessica? i want to echo the sum nomatter what it is, but the select sum(amount) if its 0 is showing up as nothing, not printing the else 0.00 statment Edited October 6, 2012 by Angeleyezz Quote Link to comment Share on other sites More sharing options...
Jessica Posted October 6, 2012 Share Posted October 6, 2012 Just run the query. No if/else, just echo the sum! Quote Link to comment Share on other sites More sharing options...
Angeleyezz Posted October 6, 2012 Author Share Posted October 6, 2012 yeah that works fine it echos the sum no problem if theres entries. but i want it to print 0.00 if theres no entries instead of just showing up blank. is there a diff way to do that besides if else? Quote Link to comment Share on other sites More sharing options...
Angeleyezz Posted October 6, 2012 Author Share Posted October 6, 2012 <!-- / END / Q2 DIESEL BREAKDOWN --> <!-- Q3 DIESEL BREAKDOWN --> <td width="25%" valign="top"><font face="verdana" size="1"><b>Q3 Diesel</b><br />$ <?php $q3_diesel = "SELECT SUM(amount) AS q3_diesel_exp FROM expense_fuel WHERE fuel_code = 001 AND year = $exp_year AND month between 07 and 09"; $q3_diesel_results = mysql_query($q3_diesel) or die(mysql_error()); while($row = mysql_fetch_array($q3_diesel_results)) { echo $row['q3_diesel_exp']; } ?> </font></td> <!-- / END / Q3 DIESEL BREAKDOWN --> this prints the sum fine no problem if there is 1, if theres none, then it just shows up blank, how would i make it show up as 0.00 echoed without an if else? im really really confused Quote Link to comment Share on other sites More sharing options...
Jessica Posted October 6, 2012 Share Posted October 6, 2012 $q1_cycle_oil = "SELECT SUM(amount) AS q1_cycle_oil_exp FROM expense_fuel WHERE fuel_code = 017 AND year = $exp_year AND month between 01 and 03"; echo "Query: $q1_cycle_oil "; $q1_cycle_oil_results = mysql_query($q1_cycle_oil) or die(mysql_error()); $row = mysql_fetch_array($q1_cycle_oil_results); echo "result: ".$row['q1_cycle_oil_exp']; print '<pre>'; print_r($row); What do you get? Quote Link to comment Share on other sites More sharing options...
Angeleyezz Posted October 6, 2012 Author Share Posted October 6, 2012 (edited) an error lol. im trying to understand how you wrote that, sucks being a noob at php Q1 Cycle Oil $ Query: SELECT SUM(amount) AS q1_cycle_oil_exp FROM expense_fuel WHERE fuel_code = 017 AND year = 2017 AND month between 01 and 03 result: Array ( [0] => [q1_cycle_oil_exp] => ) Edited October 6, 2012 by Angeleyezz Quote Link to comment Share on other sites More sharing options...
Jessica Posted October 6, 2012 Share Posted October 6, 2012 You got an error, and don't show us what it is? I wrote it using my keyboard. Quote Link to comment Share on other sites More sharing options...
Angeleyezz Posted October 6, 2012 Author Share Posted October 6, 2012 i posted it, thats what came out under cycle oil, it showed the query code, not an error error sorry Quote Link to comment Share on other sites More sharing options...
Jessica Posted October 6, 2012 Share Posted October 6, 2012 Jesus. Nevermind. Do it the way that worked. Then go read some basic tutorials. 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.