Jump to content

Adding search into a sql query


1internet

Recommended Posts

So I have a results page, e.g. a list of all the hotels for a specific city coming from the database:

"SELECT * FROM `hotels` WHERE `city`='$city'"

I want to also include a search form. They way I have tried, and assume would be the standard way is:

if(isset($_GET['search'])) {
$search = $_GET['search'];
$search = "AND `name` LIKE '%$search%'";
} else {
$search = '';
}

So it checks if the search has been set, and if it has it then creates the msql code for it. I then adjust the sql query like so:

"SELECT * FROM `hotels` WHERE `city`='$city $search'"

 

1. The search variable is not creating the correct sql result, so I think I may have done something wrong there.

2. Is this the usual way a search is added into the sql results? Would it be better to have"

 

if(isset($_GET['search'])) {
$search = $_GET['search'];
$qry = ""SELECT * FROM `hotels` WHERE `city`='$city $search'" AND `name` LIKE '%$search%'";
} else {
$qry = "SELECT * FROM `hotels` WHERE `city`='$city $search'";
}

 

Or is there an even better way?

Link to comment
https://forums.phpfreaks.com/topic/273480-adding-search-into-a-sql-query/
Share on other sites

I think it's worth bringing up that you should also sanitize what you're pulling from the $_GET array before putting it into your query.

 

For me, I usually concatenate the condition on the end instead of referencing an empty string. Small distinction, same end result.

 

Here's what I do:

 

$link = db_connect();
$query = "SELECT * FROM hotels WHERE city='{$city}'";
if ( isset($_GET['search']) )
{
  $search = make_safe($link, $_GET['search']);
  $query .= " AND name LIKE '%{$search}%'"
}
if ( $result = mysqli_query($link, $query, MYSQLI_STORE_RESULT) )
{
  // Show results
} else {
  // Handle error
}
mysqli_close($link);

function make_safe($db, $string)
{
  return mysqli_real_escape_string($db, $string);
}

The query you have won't work. You still have one of the same erros as your original post.

 

...WHERE `city`='$city $search'...

 

I would continue to concatenate:

 

$link = db_connect();
$query = "SELECT * FROM hotels WHERE city='{$city}'";
if ( isset($_GET['search']) )
{
  $search = make_safe($link, $_GET['search']);
  $query .= " AND name LIKE '%{$search}%'"
}
$query .= " ORDER BY `name` ASC"
if ( $result = mysqli_query($link, $query, MYSQLI_STORE_RESULT) )
{
  // Show results
} else {
  // Handle error
}
mysqli_close($link);

function make_safe($db, $string)
{
  return mysqli_real_escape_string($db, $string);
}

  On 1/22/2013 at 11:20 AM, Barand said:

Don't put $search inside the single quotes with $city

 

"SELECT * FROM `hotels` WHERE `city`='$city' $search"

 

  On 1/23/2013 at 10:54 AM, charlieholder said:

The query you have won't work. You still have one of the same erros as your original post.

 

...WHERE `city`='$city $search'...

 

 

Exactly so. I concluded there was no point talking to those who won't listen.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.