Jump to content

Mysql_Num_Rows Not Displaying Properly


Angeleyezz

Recommended Posts

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']; 
} 
}
?>

Link to comment
Share on other sites

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";
}
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 by tgavin
Link to comment
Share on other sites

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 by Angeleyezz
Link to comment
Share on other sites

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 =)

Link to comment
Share on other sites

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 by Angeleyezz
Link to comment
Share on other sites


<!-- / 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

Link to comment
Share on other sites

$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?

Link to comment
Share on other sites

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 by Angeleyezz
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.