Exception
Exception

Reputation: 787

how to apply LIKE %% to variable?

How can i apply LIKE to my query.

$query_event ="SELECT * FROM event_list WHERE even_title='$EventTitle' AND even_loc='$EventLocation' ";

now suppose there is form which requires either put title or put location in the form or u can put both so what will be the query?

Please help

thanks

Upvotes: 1

Views: 95

Answers (4)

JD Isaacks
JD Isaacks

Reputation: 58014

Well first, you want to prevent your SQL from injection but you can do this:

$query_event = "SELECT * FROM event_list 
   WHERE even_title LIKE '%".mysql_real_escape_string($EventTitle)."%' 
   AND even_loc LIKE '%".mysql_real_escape_string($EventLocation)."%'";

But you would actually be better of using something like PDO:

$qry = 'SELECT * FROM event_list 
   WHERE even_title LIKE :title 
   AND even_loc LIKE :location';

$data = array( 'title' => '%'.$EventTitle.'%', 
               'location' => '%'.$EventLocation.'%' );

$sth = $pdo->prepare($qry);

$sth->execute($data);

Upvotes: 1

wired00
wired00

Reputation: 14508

you want something like :

$query_event ="SELECT * FROM event_list WHERE even_title LIKE ('%".$EventTitle."%') AND even_loc LIKE ('%".$EventLocation."%')";

make sure to go through these tutorials there is a LIKE tutorial too

Upvotes: 1

Michael Berkowski
Michael Berkowski

Reputation: 270775

It is as simple as it seems. The {} are not even necessary but add readability for PHP without affecting the SQL validity.

$query_event ="SELECT * FROM event_list WHERE even_title LIKE '%{$EventTitle}%' AND even_loc LIKE '%{$EventLocation}%' ";

Upvotes: 1

Shakti Singh
Shakti Singh

Reputation: 86476

You can use following format and always escape inputs using mysql_real_escape_string

$query_event ="SELECT * FROM event_list 
              WHERE even_title LIKE '%".mysql_real_escape_string($EventTitle)."%' 
              AND  even_loc LIKE '%".mysql_real_escape_string($EventLocation)."%'";

Upvotes: 1

Related Questions