我正在尝试从表中选择一个值,但在查询中不断出现错误
这是桌子
gender range1 range2 price
male 0 100 20
male 101 200 30
female 0 100 25
female 101 199 46
这是我正在尝试的查询
$gender = $_POST["gender"];
$weight = $_POST["weight"];
$fetch = "SELECT * FROM table where gender like $gender and weight
>range1 and <range2";
$result = mysqli_query($con, $fetch) or die("error");
while ($row = $resul->fetch_assoc()) {
echo "".$row["price"]."";
}
echo $result;
答案 0 :(得分:3)
在mysql中,您可以使用BETWEEN
$fetch = "SELECT * FROM table where gender like '$gender' and (weight BETWEEN range1 AND range2)";
或者只是重复变量
$fetch = "SELECT * FROM table where gender like '$gender' AND (weight >= range1 AND weight <= range2)";
圆括号不是必需的,仅用于澄清。
答案 1 :(得分:-1)
如果要选择范围内的数据,请使用SQL BETWEEN
条件,如下所示:
SELECT * FROM table WHERE gender LIKE $gender AND weight BETWEEN range1 AND range2
另外,您应该使用准备好的语句来避免sql注入。
如果您使用的是LIKE
语句,则应该考虑添加引号(')和百分号(%)。
例如:SELECT * FROM table WHERE column LIKE '%some_data%'
或与此类似的东西