Jump to content


Photo

SQL Where clasue


  • Please log in to reply
2 replies to this topic

#1 adult.swim

adult.swim
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 12 July 2006 - 03:15 AM

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.



<?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



#2 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 12 July 2006 - 07:43 AM

The backslashes in your example are used to escape the double quotes. Alternatively you'd have:
$query = "select * from the_table where 1st_field like '%$trimmed%' order by 1st_field";


#3 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 12 July 2006 - 07:44 AM

I don't think you can use double-quotes in SQL. So try changing the query to:
select * from the_table where 1st_field like '%$trimmed%' order by 1st_field





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users