Jump to content

Archived

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

adult.swim

SQL Where clasue

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]

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites
I don't think you can use double-quotes in SQL. So try changing the query to: [code]select * from the_table where 1st_field like '%$trimmed%' order by 1st_field[/code]

Share this post


Link to post
Share on other sites

×

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.