I have a MySQL table which is something like this:
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| Weight | int | YES | | NULL | |
| 100 | double | YES | | NULL | |
| 120 | double | YES | | NULL | |
| 130 | double | YES | | NULL | |
| 150 | double | YES | | NULL | |
| 160 | double | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
I select a Row using
$row = mysqli_query($con, "SELECT * FROM Rates WHERE Weight<='$weight' AND Weight>='$weight'");
My question is using the returned query result how do I print the value of one of the Fields (100, 120, 130, 150, 160) which is closest to what the user enters.
For example is the user types in 110 the value in the field '120' would be printed. Or if 131 was entered the value of '150' would be printed (so always rounded up).
As it stands I have:
while($row = mysqli_fetch_array($rate))
{
echo "Using weight your cost is: " . $row['100'];
echo "<br>";
}
But this only prints the value of the field '100' How would I replace this to take the users input and work out which of the rows are closest and print that
So something like:
IF ($value =< 100) echo $row['100']
IF ($value =< 120 && $value > 100) echo $row['120']
IF ($value =< 130 && $value > 120) echo $row['130']
..etc
bind_parambecause what you're doing here with string interpolation can cause severe problems. It's extremely important to properly escape any and all user data to avoid SQL injection bugs.mysqlisupports parameterized queries which should be used whenever possible.