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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.