hello,
i have something like this
search:_______ search1:___________ submit btn

how can i search for something from the same table in the database considering both search fields?

I've tried this code nut it doesn't work.
thanks

if($search == '')
	{
		$smarty->assign('Error','Please enter any string!');
		$z=1;
	}
	elseif($type == 'like')
	{
		$sql = 'SELECT * FROM topic WHERE Title LIKE '."'$search%'".' AND Title1 LIKE '."'$search1%'";
	}
	elseif($type == 'exact')
	{
		$sql = 'SELECT * FROM topic WHERE MATCH (Title) AGAINST ("'.$search.'") AND (Title1) AGAINST ("'.$search1.'")';

Dani AI

Generated

Quick expert summary: was right about the quoting fix. The later symptom that described — results that look like each field was searched independently — usually comes from logic/precedence mistakes when building the WHERE clause (for example appending an OR without parentheses) or from validation code that prevents both conditions from being added. ’s question about the exact failure mode was on point; the next checks are practical and deterministic.

Troubleshooting checklist (short):

  • Log the final SQL and the exact parameter values sent to the database to verify what ran.
  • Use separate validation checks (not an if/elseif chain) so both search-conditions can be constructed.
  • Trim inputs and confirm neither variable is an empty string or contains only whitespace.
  • Be careful mixing AND and OR: SQL evaluates AND before OR, so A AND B OR C is (A AND B) OR C. Wrap groups with parentheses when needed.
  • If MATCH ... AGAINST is used, confirm a FULLTEXT index exists on the columns and be aware of stopwords and minimum-word-length behavior.

A robust pattern to build the query and avoid logical errors and injection:

$where = [];
$params = [];

if (strlen(trim($search)) ) {
    $where[] = "title LIKE ?";
    $params[] = "%$search%";
}
if (strlen(trim($search1)) ) {
    $where[] = "title1 LIKE ?";
    $params[] = "%$search1%";
}

$sql = "SELECT * FROM topic";
if ($where) {
    $sql .= " WHERE " . implode(" AND ", $where);
}

$stmt = $pdo->prepare($sql);
$stmt->execute($params);
$rows = $stmt->fetchAll();

Notes and cautions: avoid leading wildcards if index use / performance matters; use prepared statements to prevent SQL injection; for better full-text relevance search prefer MATCH(title,title1) AGAINST(... IN BOOLEAN MODE) but check MySQL fulltext settings (min word length, stopwords). For very large or complex search needs, consider a dedicated search engine (Sphinx/Elasticsearch) or tuned FULLTEXT indexes.

Recommended Answers

All 4 Replies

You used double quotes, instead of single.

if($search == '')
{
  $smarty->assign('Error','Please enter any string!');
  $z=1;
}
elseif($type == 'like')
{
  $sql = "SELECT * FROM topic WHERE Title LIKE '$search%' AND Title1 LIKE '$search1%'";
}
elseif($type == 'exact')
{
  $sql = "SELECT * FROM topic WHERE MATCH (Title) AGAINST ('$search') AND (Title1) AGAINST ('$search1')";
}

What doesn't work exactly? You get a PHP or Sql error code or you get no results when you should be?

Edit: ok, you got a solution now :] Ignore this message :]

What doesn't work exactly? You get a PHP or Sql error code or you get no results when you should be?

Edit: ok, you got a solution now :] Ignore this message :]

thanks for the reply

the single quotes improved the search, but.
it looks like the each search field finds results independent from each other. I want them to work together and the result to contain what is search in both fields

and i get no errors php or sql errors.

hello.
i believe i fix it.
ill explin my results on the code

//this is the first search field
if($search == '')
	{
		$smarty->assign('Error','Please enter an departure location!');
		$z=1;
	}
//this is the second search field
	elseif($search1 == '')
	{
		$smarty->assign('Error','Please enter any destination!');
		$z=1;
	}
//both search fields are selecting and matching the search words from the table "topic" with the fields "Title1" and "Title2"
	elseif($type == 'like')
	{
		$sql = "SELECT * FROM topic WHERE Title LIKE '$search%' AND Title1 LIKE '$search1%'";
	}
	elseif($type == 'exact')
	{
		$sql = "SELECT * FROM topic WHERE MATCH (Title) AGAINST ('$search') AND (Title1) AGAINST ('$search1')";
		//$sql .= ' UNION ';
	}
//this is what solved my problem. i replace the $search with $search1.
	if($tag == 'tag' && $z!= 1)
	{
		$sql .= ' OR Tag LIKE "%'.$search1.'%"';
	}
//and off course the double quotes thing by pritaeas:)

ill keep u all posted if any problem occurs in the nearest future
thanks

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.