2014-07-11 7 views
0

내 프로그램에는 필드의 범위가 있으며 입력 범위를 기반으로 쿼리 결과를 가져와야합니다.사이에 어떤 결과도 표시하지 않는 사이에서 mysql

아래 코드에는 여러 입력이 있으며 색상, 선명도, 모양, 자르기가 있습니다. 아래의 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입니다.

<?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

쿼리를 편집하고'변수 대체 후 $의 query_search'을 보여주십시오

시도는 괄호를 사용하여 쿼리를 고정. –

+0

데이터가 숫자 인 경우 왜'varchar' 필드를 사용합니까? 그리고 SQL 주입 문제가 있습니다. – jeroen

+0

컬럼 타입이 숫자 인 경우에 사이를 사용할 수 있습니다. – Vijayaragavendran

답변

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' "; 
+0

문제는 캐럿이 아니라 다른 분야와도 관련이 있습니다. 친구들은 문제를 일으키는 "키워드"가 너무 많다고 말합니다.하지만 확실하지 않습니다. –

+1

당신은 mysql 클라이언트에서 직접 쿼리를 실행하려했는데 어떤 일이 일어 났는가? 정확히 어떻게되는지 보려면 에코 쿼리를 사용하십시오 ... – clami219

+0

제안 사항을 적용하려고 시도 했습니까? – clami219

관련 문제