Jump to content

MYSQL search query


Xpo3

Recommended Posts

Hello I am trying to create a search query that ignore blank fields, and will search for fields that have information in them. I created this... but it is wrong, I need help.

 

    $query = "SELECT *

    FROM  residential

    WHERE bathrooms LIKE  \"$bathrooms\" OR `bathrooms`IS NULL

    AND  bedrooms LIKE  \"$bedrooms\" OR `bedrooms`IS NULL

    AND  location LIKE  \"%$location%\" OR `location`IS NULL

    AND  size LIKE  \"%$size%\" OR `size`IS NULL

    AND  cost LIKE \"%$cost%\" OR `cost`IS NULL

    AND  description LIKE\" %$description%\" OR `description`IS NULL

    AND  sold LIKE  \"%$sold%\" OR `sold`IS NULL

    AND  pool LIKE  \"%$pool%\" OR `pool`IS NULL

    AND  cargarage LIKE  \"$cargarage\" OR `cargarage`IS NULL

    AND  stories LIKE  \"$stories\" OR `stories`IS NULL

    AND  lotsize LIKE  \"%$lotsize%\" OR `lotsize`IS NULL

    AND  year LIKE  \"$year\" OR `year`IS NULL

    ORDER BY  id"; // EDIT HERE and specify your table and field names for the SQL query

 

Link to comment
https://forums.phpfreaks.com/topic/240081-mysql-search-query/
Share on other sites

Thanks very much for your reply, if I am understanding correctly it should look like this ?

$query = "SELECT *

FROM  residential

WHERE bathrooms LIKE  \"$bathrooms\" OR `bathrooms` LIKE '0'

AND  (bedrooms LIKE  \"$bedrooms\" OR `bedrooms` LIKE '0')

AND  (location LIKE  \"%$location%\" OR `location` LIKE '0')

AND  (size LIKE  \"%$size%\" OR `size` LIKE '0')

AND (cost LIKE \"%$cost%\" OR `cost` LIKE '0')

AND  (description LIKE\" %$description%\" OR `description` LIKE '0')

AND  (sold LIKE  \"%$sold%\" OR `sold` LIKE '0')

AND  (pool LIKE  \"%$pool%\" OR `pool` LIKE '0')

AND  (cargarage LIKE  \"$cargarage\" OR `cargarage`LIKE '0')

AND  (stories LIKE  \"$stories\" OR `stories` LIKE '0')

AND  (lotsize LIKE  \"%$lotsize%\" OR `lotsize` LIKE '0')

AND  (year LIKE  \"$year\" OR `year` LIKE '0')

ORDER BY  id"; // EDIT HERE and specify your table and field names for the SQL query

 

( still does not quite work)

 

I made it so if they select nothing then it will be 0

Link to comment
https://forums.phpfreaks.com/topic/240081-mysql-search-query/#findComment-1233458
Share on other sites

I meant that your columns should be non-nullable columns, by design.

 

If that is not possible with your setup (i.e., you might be maintaining an old application which relies on the "NULL mechanism"),

then your original query, w/ minor revisions, might do the trick.

 

For readability purposes, and for correctness I suggest that your query be like this:

 


$query = "SELECT *
FROM  residential
WHERE
        bathrooms LIKE  '%$bathrooms%'
OR   bedrooms LIKE  '%$bedrooms%'
OR   location LIKE  '%$location%'
OR   size LIKE  '%$size%'
....etc, etc
ORDER BY  id";

 

Replace "..etc, etc" with the remaining attributes/columns to be searched.

 

Hope it helps.

Link to comment
https://forums.phpfreaks.com/topic/240081-mysql-search-query/#findComment-1233626
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.