techker Posted September 2, 2008 Share Posted September 2, 2008 hey guys i have a query that retreives hours from my database.now i want to put an if on the query that if empty returne off.. Quote Link to comment https://forums.phpfreaks.com/topic/122415-if-question/ Share on other sites More sharing options...
fenway Posted September 2, 2008 Share Posted September 2, 2008 hey guys i have a query that retreives hours from my database.now i want to put an if on the query that if empty returne off.. Try explaining that again. Quote Link to comment https://forums.phpfreaks.com/topic/122415-if-question/#findComment-632326 Share on other sites More sharing options...
techker Posted September 3, 2008 Author Share Posted September 3, 2008 well ok.. my querry: $query9 = "SELECT COUNT(*) AS TotalCount, `to_t1` - `from_t1` + `to_t2` - `from_t2`+ `to_t3` - `from_t3`+ `to_t4` - `from_t4`+ `to_t5` - `from_t5`+`to_t6` - `from_t6`+ `to_t7` - `from_t7` AS TotalNumber, SUM( `to_t1` - `from_t1` + `to_t2` - `from_t2`+ `to_t3` - `from_t3`+ `to_t4` - `from_t4`+ `to_t5` - `from_t5`+`to_t6` - `from_t6`+ `to_t7` - `from_t7`) AS Totalhours FROM `week1`GROUP BY `from_t1` "; $result9 = mysql_query($query9) or die(mysql_error()); when when my db is empty it shows nothing.now i want it so when it is empty it shows off Quote Link to comment https://forums.phpfreaks.com/topic/122415-if-question/#findComment-632594 Share on other sites More sharing options...
Mchl Posted September 3, 2008 Share Posted September 3, 2008 if (mysql_affected_rows == 0) { echo "Off"; } else { //parse reults from $result9 } Ok... but your query will always return (at least) one row. So: $query9 = "SELECT COUNT(*) AS TotalCount, `to_t1` - `from_t1` + `to_t2` - `from_t2`+ `to_t3` - `from_t3`+ `to_t4` - `from_t4`+ `to_t5` - `from_t5`+`to_t6` - `from_t6`+ `to_t7` - `from_t7` AS TotalNumber, SUM( `to_t1` - `from_t1` + `to_t2` - `from_t2`+ `to_t3` - `from_t3`+ `to_t4` - `from_t4`+ `to_t5` - `from_t5`+`to_t6` - `from_t6`+ `to_t7` - `from_t7`) AS Totalhours FROM `week1`GROUP BY `from_t1` "; $result9 = mysql_query($query9) or die(mysql_error()); $row = mysql_fetch_array($result9); if ($row['TotalCount'] == 0) { echo "off"; } else { // } Quote Link to comment https://forums.phpfreaks.com/topic/122415-if-question/#findComment-632598 Share on other sites More sharing options...
techker Posted September 3, 2008 Author Share Posted September 3, 2008 ok small question for me to understand once and for all!lolsorry that querry is in the top of my page.now in my page were the echo is i put this <? while($row = mysql_fetch_array($result9)) {?> <? echo $row['Totalhours'] ." H"; ?> <? }?> now do i put that if there? <? while($row = mysql_fetch_array($result9)) {?> if (mysql_affected_rows == 0) { echo "Off"; } else { <? echo $row['Totalhours'] ." H"; ?> } Quote Link to comment https://forums.phpfreaks.com/topic/122415-if-question/#findComment-632607 Share on other sites More sharing options...
Mchl Posted September 3, 2008 Share Posted September 3, 2008 One question, cause I'm getting confused myself the more I think about it. When do you want 'Off' to be displayed? When there are no rows in 'week1' table or when you can't connect to database server? Quote Link to comment https://forums.phpfreaks.com/topic/122415-if-question/#findComment-632610 Share on other sites More sharing options...
techker Posted September 3, 2008 Author Share Posted September 3, 2008 when there is no row(no results,nothing in there) Quote Link to comment https://forums.phpfreaks.com/topic/122415-if-question/#findComment-632613 Share on other sites More sharing options...
Mchl Posted September 3, 2008 Share Posted September 3, 2008 So it's pretty much like you did in your previouspost <? if (mysql_affected_rows == 0) { echo "Off"; } else { while($row = mysql_fetch_array($result9)) { echo $row['Totalhours'] ." H"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/122415-if-question/#findComment-632623 Share on other sites More sharing options...
techker Posted September 3, 2008 Author Share Posted September 3, 2008 nice thx!i have a hard time with the mixing of html and php.. Quote Link to comment https://forums.phpfreaks.com/topic/122415-if-question/#findComment-632634 Share on other sites More sharing options...
Mchl Posted September 3, 2008 Share Posted September 3, 2008 You don't have to use <?php ?> tags in each line. Just put them around php code. Will make your code easier to read. Quote Link to comment https://forums.phpfreaks.com/topic/122415-if-question/#findComment-632636 Share on other sites More sharing options...
techker Posted September 3, 2008 Author Share Posted September 3, 2008 its giving me Parse error: syntax error, unexpected T_STRING in /home/techker/public_html/PUNCH/index2.php on line 70 Quote Link to comment https://forums.phpfreaks.com/topic/122415-if-question/#findComment-632638 Share on other sites More sharing options...
Mchl Posted September 3, 2008 Share Posted September 3, 2008 What's in line 70 (and around it). Probably missing ; in the line above. Quote Link to comment https://forums.phpfreaks.com/topic/122415-if-question/#findComment-632648 Share on other sites More sharing options...
techker Posted September 3, 2008 Author Share Posted September 3, 2008 its the line that you made. Quote Link to comment https://forums.phpfreaks.com/topic/122415-if-question/#findComment-632668 Share on other sites More sharing options...
Mchl Posted September 3, 2008 Share Posted September 3, 2008 Which one? Quote Link to comment https://forums.phpfreaks.com/topic/122415-if-question/#findComment-632695 Share on other sites More sharing options...
techker Posted September 3, 2008 Author Share Posted September 3, 2008 <? if (mysql_affected_rows == 0) { echo "Off"; } else { while($row = mysql_fetch_array($result9)) { echo $row['Totalhours'] ." H"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/122415-if-question/#findComment-632702 Share on other sites More sharing options...
Mchl Posted September 3, 2008 Share Posted September 3, 2008 These are 9 lines... which one is #70? Anyways, error's here: if (mysql_affected_rows() == 0) { Quote Link to comment https://forums.phpfreaks.com/topic/122415-if-question/#findComment-632707 Share on other sites More sharing options...
fenway Posted September 3, 2008 Share Posted September 3, 2008 This is degenerating into a php-only problem... if it's not solved soon, I'll move it. Quote Link to comment https://forums.phpfreaks.com/topic/122415-if-question/#findComment-632820 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.