abdfahim Posted September 22, 2007 Share Posted September 22, 2007 What I want is to replace all values from a result variable which are less than a certain threshold with that threshold. For example, say a sample php code is $result=mysql_query("SELECT drop_rate from sTable WHERE field='DH001122'"); Now I want to replace values from $result which is less than 50, with 50. For example, if a value is 40, it will become 50, but if value is 60, it will remain 60. Can this be done in php? Quote Link to comment Share on other sites More sharing options...
almightyegg Posted September 22, 2007 Share Posted September 22, 2007 I'm not 100% sure what you're asking but if I'm right I would do this: <? $result=mysql_query("SELECT * from sTable WHERE field='DH001122'"); // noticed I changed the original query while($res = mysql_fetch_array($result)){ if($res[drop_rate] < 50){ $drop_rate = 50; }elseif($res[drop_rate] >= 50){ $drop_rate = $res[drop_rate]; } $update = mysql_query("UPDATE sTable SET drop_rate='$drop_rate' WHERE field='DH001122'") or die(mysql_error()); } Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted September 22, 2007 Share Posted September 22, 2007 one sql statement: UPDATE sTable SET drop_rate = 50 WHERE drop_rate < 50 Quote Link to comment Share on other sites More sharing options...
abdfahim Posted September 22, 2007 Author Share Posted September 22, 2007 Ok, this is my fault that I can't clear the scenario. Actually, I don't want to update the table. I just want to update the $result variable. I don't know, whether a result variable can be manipulated or not. That's why I am just checking it with experts. Quote Link to comment Share on other sites More sharing options...
MmmVomit Posted September 22, 2007 Share Posted September 22, 2007 You can use the IF function in your query. $sql = "SELECT IF(drop_rate <= 50, 50, drop_rate) AS drop_rate FROM sTable WHERE field='DH001122';"; $result = mysql_query($sql); http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html 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.