0

I'm currently coding a simple search script in PHP that requires three variables from the user namely: $Capacity, $Location and $RoomType

Capacity is a required field which the jquery validate plugin checks for numerical entry on input - but Location and RoomType are optional.

I'm trying to now draft a SQL query that will search the table rooms.

There are three columns in the table also called Capacity, Location and RoomType that I want to search using the variables.

How would I write this SQL query? Especially with $Capacity being required, $Location / $RoomType expected to be left blank or filled in at the users discretion?

4 Answers 4

1

You could use LIKE ...% in your sql query, so that even when blank, it'll be treated as a wildcard.

$q = 'SELECT * FROM foo WHERE capacity = "'.$capacity.'" AND location LIKE "'.$location.'%" AND roomtype LIKE "'.$roomtype.'%"';

Of course, remember to escape the inputs.

Sign up to request clarification or add additional context in comments.

2 Comments

How do I check whether the Capacity variable is in a specific range for example if the user types in 50 then the entry in the table is 100 then it falls in range?
In php, you can add $capacityA = $capacity - 50; $capacityB = $capacity + 50; and in MySQL change it to WHERE capacity BETWEEN $capacityA AND $capacityB
0

Something like this should work:

function BuildSearchQuery($Capacity, $Location, $RoomType)
{

    $where = array();

    if (!empty($Capacity))
    {
        $where[] = "Capacity = '" . mysql_real_escape_string($Capacity) . "'";
    }
    if (!empty($Location))
    {
        $where[] = "Location = '" . mysql_real_escape_string($Location) . "'";
    }
    if (!empty($RoomType))
    {
        $where[] = "RoomType = '" . mysql_real_escape_string($RoomType) . "'";
    }

    if (empty($where))
    {
        return false;
    }

    $sql = "select * from `table` where ";
    $sql += implode(" AND ", $where);

    return $sql;

}

Although nowadays many frameworks exists that allow you to do this more easily and less error-prone than manually crafting queries.

1 Comment

I'm using Dreamweaver can you recommend any that I can use?
0

$query =select * from table where Capacity =$Capacity

if(isset($Location) && $Location!='') $query.= and Location LIKE '%$location%'

if(isset($RoomType) && $RoomType!='') $query.= and RoomType LIKE '%$RoomType%'

Making use of LIKE or = operator in query is upto you.

1 Comment

if(isset($Location) && $Location!='') is equivalent to if(!empty($location))
0

Depend on how complex it is (and or not ???)

But basically

Select ... From Rooms Where Capacity = @Capacity 
and ((Location = @Location) Or (IsNull(@Location,'') = '')) 
and ((RoomType = @RoomType) or (IsNull(@RoomType,'') = ''))

Or some such.

If you aren't using parameterised queryies then replace @.... with the escaped inputs.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.