Hi guys, well as the thread title says, i've made a search engine for a property site that uses a form to specify what to search (ie. property status, type, location, bedrooms, price). And outputs as a table.

The problem is i don't know how to make the function return the results that i've specifically searched for, currently my php coding goes like this:

<?php
// form variables
if (isset($_POST)) {$status = $_POST; } else { $status = ""; }
if (isset($_POST)) {$prop_type = $_POST; } else { $prop_type = ""; }
if (isset($_POST)) {$location = $_POST; } else { $location = ""; }
if (isset($_POST)) {$bedrooms = $_POST; } else { $bedrooms = ""; }
if (isset($_POST)) {$min_price = $_POST; } else { $min_price = ""; }
if (isset($_POST)) {$max_price = $_POST; } else { $max_price = ""; }

mysql_select_db($database, $dbconnect);
$query_dosearch = "SELECT * FROM ".$sitename."_properties WHERE status = '$status' OR property_type = '$prop_type' OR location = '$location' OR num_bedrooms = '$bedrooms' OR price BETWEEN '$min_price' AND '$max_price' ";
$dosearch = mysql_query($query_dosearch, $dbconnect) or die(mysql_error($dbconnect));
$row_dosearch = mysql_fetch_assoc($dosearch);
$totalRows_dosearch = mysql_num_rows($dosearch);

?>

I've tried using AND but that returns no results, so can anyone help me?

Dani AI

Generated

— the root cause is the way the WHERE clause is being built. Using OR will return rows that match any single filter, while AND requires all conditions — but the real issue is including empty form values in the query. The posted code also checks isset($_POST) instead of each key (for example isset($_POST['status'])), so fields look "set" even when blank. Only add a condition to the WHERE clause when that particular input is present and valid.

A safe, modern approach is to build an array of conditions and a matching array of parameters, then join the conditions with AND. Use prepared statements so values are bound instead of interpolated. For price ranges only add a BETWEEN/>=/<= clause when min and/or max are numeric.

$conds = []; $params = [];
if (!empty($_POST['status'])) { $conds[] = 'status = :status'; $params[':status'] = $_POST['status']; }
if (!empty($_POST['property_type'])) { $conds[] = 'property_type = :type'; $params[':type'] = $_POST['property_type']; }
if (!empty($_POST['location'])) { $conds[] = 'location = :loc'; $params[':loc'] = $_POST['location']; }
if (isset($_POST['min_price']) && $_POST['min_price']!=='') { $conds[] = 'price >= :min'; $params[':min'] = (float)$_POST['min_price']; }
if (isset($_POST['max_price']) && $_POST['max_price']!=='') { $conds[] = 'price <= :max'; $params[':max'] = (float)$_POST['max_price']; }

$sql = 'SELECT * FROM properties' . ($conds ? ' WHERE '.implode(' AND ', $conds) : '');
$stmt = $pdo->prepare($sql);
$stmt->execute($params);
$results = $stmt->fetchAll();

Follow 's hint — ensure a valid DB connection — and take 's debugging tip further: log or var_dump both the final SQL and the bound parameters before execute. Also validate and cast numeric inputs, only include price clauses when they make sense (min <= max), add appropriate indexes on the filtered columns, and paginate results to avoid huge result sets.

Recommended Answers

All 2 Replies

As far as I know, there is no need of :

mysql_query($query_dosearch, $dbconnect)
You can simply do
mysql_query($query_dosearch);

Also I do not see a function mysql_connect() or mysql_pconnect() which connects to the database? Have you forgotten that ?

man echo the sql syntax
sometimes there is errors u cant c until u echo the sql
it is a good habit

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.