在我的程序中,有一系列字段,我需要根据输入范围从查询中获取结果。
在下面的代码中,有多个输入,它们是颜色,清晰度,形状,切割。在下面的php代码中,我从用户获取范围,然后使用mysql查询根据查询查找所有可能的结果。
例如,在克拉字段中包含从1到25的值,如果用户输入2到10,则数据库应显示从2到10的所有字段。其他字段也是如此。
场颜色包括D,E,F,G,H,I,J,K等值。清晰度包括FL,IF,VVS1,VVS2,VS1,VS2,SL1,SL2等值。
我希望我的查询找到给定范围内的所有结果,然后显示结果。我的问题是,虽然数据库中包含确切的数据,但没有显示任何内容。
数据库的所有字段都是varchar,除了carat是数字。
<?php
$hostname_localhost ="localhost";
$database_localhost ="testdb";
$username_localhost ="root";
$password_localhost ="";
$localhost = mysql_connect($hostname_localhost,$username_localhost,$password_localhost)
or
trigger_error(mysql_error(),E_USER_ERROR);
mysql_select_db($database_localhost, $localhost);
$carat1 = $_POST['carat1'];
$carat2 = $_POST['carat2'];
$clarity1 = $_POST['clarity1'];
$clarity2 = $_POST['clarity2'];
$color1 = $_POST['color1'];
$color2 = $_POST['color2'];
$cut1 = $_POST['cut1'];
$cut2 = $_POST['cut2'];
$shape1 = $_POST['shape1'];
$shape2 = $_POST['shape2'];
$stones = $_POST['stones'];
$query_search ="Select * from search1 where carats Between '$carat1' and '$carat2' and
color Between'$color1' and '$color2' and cut Between '$cut1' and '$cut2' and shape Between '$shape1' and '$shape2' and stone ='$stones' ";
$query_exec = mysql_query($query_search) or die(mysql_error());
while($row=mysql_fetch_assoc($query_exec))
$json_output[]=$row;
print(json_encode($json_output));
mysql_close();
//$rows = mysql_num_rows($query_exec);
//echo $rows;
//while($row=mysql_fetch_assoc($query_exec)) { $json_output[]=$row; } echo json_encode($json_output);
?>
如果我将查询更改为然后它会给出结果,但它不是范围。
$query_search ="Select * from search1 where carats Between '$carat1' and '$carat2' and
color = '$color1' or color = '$color2' and cut = '$cut1' or cut = '$cut2' and shape = '$shape1' or shape = '$shape2' and stone ='$stones' ";
答案 0 :(得分:0)
我看到的一个问题是carat
字段可能是db上的数字(因为你说它是一个数值)。如果是这样,在您的查询中你应该有:
... where carats Between $carat1 and $carat2 ...
没有引号。
尝试使用括号修复查询:
$query_search ="Select * from search1 where (carats Between '$carat1' and '$carat2') and (color Between'$color1' and '$color2') and (cut Between '$cut1' and '$cut2') and (shape Between '$shape1' and '$shape2') and stone ='$stones' ";