Jump to content

SQL Where clasue


adult.swim

Recommended Posts

I've just found the code I've been looking for that Uses PHP to search a MySQL database and return paged results.

I've been modifying the code to fit the specifiactions that I need. My question is what is the significance of:

\"%$trimmed%\"

in the SQL Query. I dont know how the backslashs work, or what they mean in this context. If u know please explain, Thank You.


[code=php:0]

<?php

  // Get the search variable from URL
  $var = @$_GET['q'] ;
  $trimmed = trim($var); //trim whitespace from the stored variable

// rows to return
$limit=10;

// check for an empty string and display a message.
if ($trimmed == "")
  {
  echo "<p>Please enter a search...</p>";
  exit;
  }

// check for a search parameter
if (!isset($var))
  {
  echo "<p>We dont seem to have a search parameter!</p>";
  exit;
  }

//connect to your database ** EDIT REQUIRED HERE **
mysql_connect("localhost","username","password"); //(host, username, password)

//specify database ** EDIT REQUIRED HERE **
mysql_select_db("database") or die("Unable to select database"); //select which database we're using

// Build SQL Query 
$query = "select * from the_table where 1st_field like \"%$trimmed%\" 
  order by 1st_field"; // EDIT HERE and specify your table and field names for the SQL que

[/code]
Link to comment
Share on other sites

The backslashes in your example are used to escape the double quotes. Alternatively you'd have:
[code=php:0]$query = "select * from the_table where 1st_field like '%$trimmed%' order by 1st_field";[/code]
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.