2017-04-03 5 views
0

사용자가 일부 정보를 입력 할 수있는 양식이 있습니다. 양식을 제출 한 후 사용자가 입력 한 값으로 데이터베이스를 조회해야합니다.MYSQL 쿼리에서 null 값 무시

여기 내 문제는 사용자가 입력 한 값 중 일부가 null이면 쿼리에서 제거해야한다는 것입니다.

이 내 코드입니다 : $negocio, $imovel, $concelho$freguesianull에 동일한 경우

if(isset($_POST['submit'])) 
{ 
include("../includes/header.php"); 
include ("../scripts/db/connect.php"); 

//Gets variables from $_POST 
$negocio = $_POST['negocio']; 
$imovel = $_POST['imovel']; 
$distrito = $_POST['distrito']; 
$concelho = $_POST['concelho']; 
$freguesia = $_POST['freguesia']; 

$query = "SELECT * FROM imoveis WHERE negocio = $negocio and imovel = $imovel and distrito = $distrito and concelho = $concelho and freguesia = $freguesia"; 
} 

상상 쿼리가 있어야한다 :

$query = "SELECT * FROM imoveis WHERE distrito = $distrito; 

어떻게하면 할 수

?

+0

당신은 함께, OR'의 대신 AND'의'의'사용하거나 CASE와 하위 쿼리를 사용할 수 있습니다 'IS NULL'을 사용합니다. 변수가 문자열인지 여부와 db 스키마가 명확하지 않습니다. –

+0

죄송합니다. 제 변수는 단지 int입니다. –

+0

Alexandre 두 가지 대답이 있는데 둘 다 맞다면 혼란 스럽다면 다시 한 번 읽으십시오. –

답변

3

쿼리 문자열이 dynamcilly 제거하거나 후, 값이 여부가 null의 설정에 따라, 그 쿼리를

실행이 점을 이해하는 별도의 파일에이 코드를 사용하여보다 생성 어떤 변수 ($ 이름, $ 거리, $ 주소 또는 $ 자격)에 주석을 추가하는 것은

// you will see query will change depending on the set variable, 
//I am using these name you can use any name for your variable 

$name='my name'; 
//$address='some where on earth'; 
$street='this is my street'; 
//$qualification='i am very much qualified'; 

//now create the array only with the values which are not empty or not nul, 
//I am using empty you can use null if you want with this example you can use any thing. 
if(!empty($name)) $query_string_second_part[]=" AND name = '$name'"; 
if(!empty($address)) $query_string_second_part[]=" AND address = '$address'"; 
if(!empty($street)) $query_string_second_part[]=" AND street = '$street'"; 
if(!empty($qualification)) $query_string_second_part[]=" AND qualification = '$qualification'"; 

//hand type the first part for the query 
$query_string_First_Part= "SELECT * FROM myTableName WHERE"; 
//Implode the array, if you want to see how it look like use echo, 
$query_string_second_part= implode(" ", $query_string_second_part); 
//as you can see we are adding AND with every value, so we need to remove the first AND 
//with one space 
//Make sure you give space in the second parameter. else it wont work means "" not correct but " " is correct 
//Hint --> use one space in between the double qoutes 
$query_string_second_part= preg_replace("/AND/", " ", $query_string_second_part, 1); 

//Join the first and second part together to create a full query 
$query_string=$query_string_First_Part.$query_string_second_part; 
echo ($query_string);//see how our query look like at the moment 
0

각 절에 입력 널 (null) 검사를 추가 할 수 있습니다. 이 그래서 예를 들어 당신이 여기서

distrito = $distrito 

대신이 작업을 수행 할 수 있습니다 아마도

(distrito = $distrito or $distrito IS NULL) 

나 :

(distrito = $distrito or $distrito = '') 

를 데이터 유형에 따라 실제 입력에 사용되는 쿼리를 작성하는 등 수동으로 쿼리를 구축 할 때 약간의 조정 및 디버깅이 필요할 수 있습니다 (쿼리 매개 변수가있는 준비된 문을 사용하면 의심의 여지가 있지만 더 안전함). 아이디어는 같은 방법입니다.

기본적으로 값에 따라 행을 일치 시키거나 값의 부족에 따라 행을 일치 시키도록 지시하고 있습니다. 따라서 주어진 절에 대해 제공된 값이 null/empty이면 모든 행이 일치하고 절은 moot가됩니다.

+0

나는 당신의 생각을 이해하지 못한다. null 값으로 쿼리하려고하면 null 값을 가진 결과가 반환됩니다. –

+0

@AlexandreCristo : 아이디어는 'NULL'값을 제공하면 * all * 행이 반환됩니다. 검색 값이 not로 해석되기 때문입니다 검색에 사용되었습니다. 주어진 값에 대해'(distrito = $ distrito 또는 $ distrito IS NULL)'을 고려하십시오.'NULL' 또는 기타. – David