robinhood Posted September 3, 2007 Share Posted September 3, 2007 $timecounter = mysql_query("select timecounter from admin where username='$username' "); $result=mysql_query("select * from admin where username='$username' "); if(mysql_num_rows($result)!='0'){ // If match. //inserting time into database $hourdiff = "8"; $timeadjust = ($hourdiff * 3600); $b = (time() + $timeadjust); $c = date("g:i:s A D, F jS Y",$b); "below is it if and else function" if ($timecounter != "1"){ $update = mysql_query("UPDATE admin SET timein = '$c' where username='$username' "); $update = mysql_query("UPDATE admin SET timecounter='1' where username='$username' "); } else{ $update = mysql_query("UPDATE admin SET timeout = '$c' where username='$username' "); $update = mysql_query("UPDATE admin SET timecounter='0' where username='$username' "); } The if part was able to execute where timecounter was able to set to 1 and timein set to $c but was not able to execute else part..is there anything wrong which my statements. Quote Link to comment https://forums.phpfreaks.com/topic/67735-solved-help-needed-if-and-else-functioncan-only-execute-if-statement-but-not-else/ Share on other sites More sharing options...
MrXander Posted September 3, 2007 Share Posted September 3, 2007 You need to close your bery first IF statement... Try this: $timecounter = mysql_query("select timecounter from admin where username='$username' "); $result=mysql_query("select * from admin where username='$username' "); if(mysql_num_rows($result)!='0'){ // If match. //inserting time into database $hourdiff = "8"; $timeadjust = ($hourdiff * 3600); $b = (time() + $timeadjust); $c = date("g:i:s A D, F jS Y",$b); "below is it if and else function" if ($timecounter != "1"){ $update = mysql_query("UPDATE admin SET timein = '$c' where username='$username' "); $update = mysql_query("UPDATE admin SET timecounter='1' where username='$username' "); } else{ $update = mysql_query("UPDATE admin SET timeout = '$c' where username='$username' "); $update = mysql_query("UPDATE admin SET timecounter='0' where username='$username' "); } } Quote Link to comment https://forums.phpfreaks.com/topic/67735-solved-help-needed-if-and-else-functioncan-only-execute-if-statement-but-not-else/#findComment-340298 Share on other sites More sharing options...
btherl Posted September 3, 2007 Share Posted September 3, 2007 Also, $timecounter is a result, not a number. $timecounter_result = mysql_query("select timecounter from admin where username='$username' "); $timecounter = mysql_fetch_result($timecounter_result, 0, 'timecounter'); print "Timecounter has value $timecounter for user $username<br>"; Quote Link to comment https://forums.phpfreaks.com/topic/67735-solved-help-needed-if-and-else-functioncan-only-execute-if-statement-but-not-else/#findComment-340318 Share on other sites More sharing options...
trq Posted September 3, 2007 Share Posted September 3, 2007 Also, numbers should not be in quotes. This terns them into strings. So this... if ($timecounter != "1"){ should be.... if ($timecounter != 1) { Quote Link to comment https://forums.phpfreaks.com/topic/67735-solved-help-needed-if-and-else-functioncan-only-execute-if-statement-but-not-else/#findComment-340372 Share on other sites More sharing options...
robinhood Posted September 4, 2007 Author Share Posted September 4, 2007 tks for the help trying now.. Quote Link to comment https://forums.phpfreaks.com/topic/67735-solved-help-needed-if-and-else-functioncan-only-execute-if-statement-but-not-else/#findComment-341035 Share on other sites More sharing options...
teng84 Posted September 4, 2007 Share Posted September 4, 2007 what do you mean bro if else terminate when it satisfies on of the condition can you explain your BIG prob Quote Link to comment https://forums.phpfreaks.com/topic/67735-solved-help-needed-if-and-else-functioncan-only-execute-if-statement-but-not-else/#findComment-341037 Share on other sites More sharing options...
TheFilmGod Posted September 4, 2007 Share Posted September 4, 2007 if ($timecounter != "1"){ $update = mysql_query("UPDATE admin SET timein = '$c' where username='$username' "); $update = mysql_query("UPDATE admin SET timecounter='1' where username='$username' "); } else{ $update = mysql_query("UPDATE admin SET timeout = '$c' where username='$username' "); $update = mysql_query("UPDATE admin SET timecounter='0' where username='$username' "); } This is completely not smart.They are almost the same except the 1 an 0 part. Why not do it like this: if ( $timecounter != 1) { $timecounter = 1; } else { $timecounter = 0; } $update = mysql_query("UPDATE admin SET timeout = '$c' where username='$username' "); $update = mysql_query("UPDATE admin SET timecounter='$timecounter' where username='$username' "); Your code wasn't that bad. You just need to understand that if you are manipulating a variable you manipulate it. Dont' manipulate it by a mysql query. Your way uses more code. Mine is shorter and does the same thing. Quote Link to comment https://forums.phpfreaks.com/topic/67735-solved-help-needed-if-and-else-functioncan-only-execute-if-statement-but-not-else/#findComment-341045 Share on other sites More sharing options...
TheFilmGod Posted September 4, 2007 Share Posted September 4, 2007 whoops! I couldn't modify my post so I"m making a new one, sorry: You can make it EVEN shorter: if ( $timecounter != 1) { $timecounter = 1; } else { $timecounter = 0; } $update = mysql_query("UPDATE admin SET timeout = '$c', timecounter='$timecounter' where username='$username' "); You can update the two variables at the same time. $timecounter = mysql_query("select timecounter from admin where username='$username' "); $result=mysql_query("select * from admin where username='$username' "); INTO --> $row = mysql_query("select timecounter from admin where username='$username' "); You must understand that mysql selects "rows" not cells. You then do mysql_fetch_assoc or mysql_fetch_row to get separate data from a row. Hope this helps. Quote Link to comment https://forums.phpfreaks.com/topic/67735-solved-help-needed-if-and-else-functioncan-only-execute-if-statement-but-not-else/#findComment-341047 Share on other sites More sharing options...
robinhood Posted September 4, 2007 Author Share Posted September 4, 2007 tks it works...a lot more to learn... Quote Link to comment https://forums.phpfreaks.com/topic/67735-solved-help-needed-if-and-else-functioncan-only-execute-if-statement-but-not-else/#findComment-341079 Share on other sites More sharing options...
robinhood Posted September 4, 2007 Author Share Posted September 4, 2007 $timecounter_result = mysql_query("select timecounter from admin where username='$username' "); //Fatal error: Call to undefined function mysql_fetch_result() in C:\xampp\htdocs\chua\login\index.php on line 38 // This line below is line 38 // $timecounter = mysql_fetch_result($timecounter_result, 0, 'timecounter'); if($timecounter==0){ $update = mysql_query("UPDATE admin SET timein = '$c',timecounter='1' where username='$username' "); } else{ $update2 = mysql_query("UPDATE admin SET timeout = '$c',timecounter='0' where username='$username' "); } anyone 1 knows what happened? Quote Link to comment https://forums.phpfreaks.com/topic/67735-solved-help-needed-if-and-else-functioncan-only-execute-if-statement-but-not-else/#findComment-341138 Share on other sites More sharing options...
Jessica Posted September 4, 2007 Share Posted September 4, 2007 your query is failing. Try this: <?php $sql = "select timecounter from admin where username='$username'"; $timecounter_result = mysql_query($sql) OR die(mysql_error().' Query: '.$sql); Quote Link to comment https://forums.phpfreaks.com/topic/67735-solved-help-needed-if-and-else-functioncan-only-execute-if-statement-but-not-else/#findComment-341140 Share on other sites More sharing options...
btherl Posted September 4, 2007 Share Posted September 4, 2007 Sorry, that's my mistake. Try this instead of mysql_fetch_result(): $row = mysql_fetch_array($timecounter_result); $timecounter = $row['timecounter']; Quote Link to comment https://forums.phpfreaks.com/topic/67735-solved-help-needed-if-and-else-functioncan-only-execute-if-statement-but-not-else/#findComment-341173 Share on other sites More sharing options...
robinhood Posted September 4, 2007 Author Share Posted September 4, 2007 anyway tks for following up with me..a bit still lost $timecounter_result = "select timecounter from admin where username='$username'"; $row = mysql_fetch_array($timecounter_result);////////////////This is line 38//////////////////// $timecounter = $row['timecounter']; if($timecounter==0){ $update = mysql_query("UPDATE admin SET timein = '$c',timecounter='1' where username='$username' "); print "$timecounter2"; } else{ $update2 = mysql_query("UPDATE admin SET timeout = '$c',timecounter='0' where username='$username' "); } Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\chua\login\index.php on line 38 Warning: session_register() [function.session-register]: Cannot send session cache limiter - headers already sent (output started at C:\xampp\htdocs\chua\login\index.php:38) in C:\xampp\htdocs\chua\login\index.php on line 51 Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\chua\login\index.php:38) in C:\xampp\htdocs\chua\login\index.php on line 52 Warning: Unknown: Your script possibly relies on a session side-effect which existed until PHP 4.2.3. Please be advised that the session extension does not consider global variables as a source of data, unless register_globals is enabled. You can disable this functionality and this warning by setting session.bug_compat_42 or session.bug_compat_warn to off, respectively. in Unknown on line 0 Quote Link to comment https://forums.phpfreaks.com/topic/67735-solved-help-needed-if-and-else-functioncan-only-execute-if-statement-but-not-else/#findComment-341178 Share on other sites More sharing options...
robinhood Posted September 4, 2007 Author Share Posted September 4, 2007 hmmm, or any helps for doing it a better way i doing -time in and time out. -using a 'time counter' default '0' when scaning it will checks 'time counter' = 0 or 1 if 'time counter' 0 = scan time in and sets 'time counter' =1 elseif time counter 1=scan 'time out' and sets 'time counter' = 0 am i doing correctly.. Quote Link to comment https://forums.phpfreaks.com/topic/67735-solved-help-needed-if-and-else-functioncan-only-execute-if-statement-but-not-else/#findComment-341186 Share on other sites More sharing options...
Jessica Posted September 4, 2007 Share Posted September 4, 2007 your query is failing. Try this: <?php $sql = "select timecounter from admin where username='$username'"; $timecounter_result = mysql_query($sql) OR die(mysql_error().' Query: '.$sql); Quote Link to comment https://forums.phpfreaks.com/topic/67735-solved-help-needed-if-and-else-functioncan-only-execute-if-statement-but-not-else/#findComment-341279 Share on other sites More sharing options...
btherl Posted September 5, 2007 Share Posted September 5, 2007 Here is the full thing, from query to result: $sql = "select timecounter from admin where username='$username'"; $timecounter_result = mysql_query($sql) OR die(mysql_error().' Query: '.$sql); $row = mysql_fetch_array($timecounter_result); $timecounter = $row['timecounter']; Quote Link to comment https://forums.phpfreaks.com/topic/67735-solved-help-needed-if-and-else-functioncan-only-execute-if-statement-but-not-else/#findComment-341715 Share on other sites More sharing options...
robinhood Posted September 5, 2007 Author Share Posted September 5, 2007 tks btherl, jesirose and all others who help..totally appreciate Quote Link to comment https://forums.phpfreaks.com/topic/67735-solved-help-needed-if-and-else-functioncan-only-execute-if-statement-but-not-else/#findComment-341845 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.