Aparistar
Aparistar

Reputation: 185

Find column name which matches multiple column search query

I Have a simple search query which works just fine

$result = mysql_query("SELECT id, $column, $column2 FROM $table WHERE BINARY account_no='{$_SESSION['account']}' AND $column LIKE '$query' OR $column2 LIKE '$query'") or die(mysql_error());

$output_column=column1 or column2 //which column has been matched?

However when there is a match on a particulat column i would like to be able to find out which column has been matched.

Is this possible?

Upvotes: 0

Views: 110

Answers (1)

Legionar
Legionar

Reputation: 7607

Yes, just edit your query like this:

$result = mysql_query("
    SELECT 
        id, $column, $column2,
        IF ($column LIKE '$query', 1, 0) AS `column_true`,
        IF ($column2 LIKE '$query', 1, 0) AS `column2_true`
    FROM $table
    WHERE BINARY account_no='{$_SESSION['account']}' AND ($column LIKE '$query' OR $column2 LIKE '$query')")
or die(mysql_error());

so you will fetch also another 2 data field. if column_true = 1, first column condition was true if column2_true = 1, second column condition was true

if both are 1, the both column and column2 matched your condition.

Btw: you have error in your query, missing ( ) around OR condition

Upvotes: 2

Related Questions