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
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);
}

Edited by charlieholder
Link to comment
Share on other sites

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);
}

Link to comment
Share on other sites

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

 

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

 

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.