Jump to content

replace values less than a threshold


abdfahim

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/70251-replace-values-less-than-a-threshold/
Share on other sites

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());

}

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.

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.