Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 01-14-2007, 05:02 AM   #1 (permalink)
markster
Code Monkey
 
markster's Avatar
 
Join Date: Jun 2006
Posts: 65
markster is on a distinguished road
Making a script serach a database

i have a database of products and i want to search them by description depending on what a user puts into a text field. problem is i havent a clue how to do it. i figure it would need to split the search term into an array word by word, and then i run out of ideas. anyone got any ideas?
markster is offline   Reply With Quote
Old 01-14-2007, 05:38 AM   #2 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 673
DJMaze is on a distinguished road
Code:
SELECT * FROM table WHERE field LIKE '%word%'
is an option
__________________

UT: Ultra-kill... God like!
DJMaze is offline   Reply With Quote
Old 01-14-2007, 08:33 PM   #3 (permalink)
waveclaw
Recruit
 
waveclaw's Avatar
 
Join Date: Jul 2006
Location: USA
Posts: 17
waveclaw is on a distinguished road
Send a message via ICQ to waveclaw Send a message via AIM to waveclaw Send a message via MSN to waveclaw Send a message via Yahoo to waveclaw
Which DB?

Quote:
Originally Posted by markster View Post
i have a database of products
What kinda of a database? A MySQL database? sqlite? Postgresql? Oracle? If you have a relational database then you can use DJMaze's SQL (Structured Query Language) statement. PHP has support for SQL to access a number of databases.

Otherwise, as with a flat-file, you can look up any number of search algorithms online.

Be careful with accepting input from a user to search any database. If you directly run the search with input from the user then they can inject extra SQL into your search. These extra commands can possibly run any SQL operation on your database that your PHP system can, including deleting the whole database. Some database utility libraries may offer to handle this for you, like prepared statements, or completely ignore it forcing you to write security code directly into your application.
waveclaw is offline   Reply With Quote
Old 01-15-2007, 06:01 AM   #4 (permalink)
markster
Code Monkey
 
markster's Avatar
 
Join Date: Jun 2006
Posts: 65
markster is on a distinguished road
i need to accommodate multiple word searches, and possibly list by relativity.
markster is offline   Reply With Quote
Old 01-15-2007, 10:39 AM   #5 (permalink)
toe_cutter
Code Monkey
 
Join Date: Aug 2002
Location: Boston, MA
Posts: 79
toe_cutter is on a distinguished road
Send a message via ICQ to toe_cutter Send a message via AIM to toe_cutter Send a message via Yahoo to toe_cutter
You will need to create a function that creates a SQL statement like DJMaze suggested.

I pre-populate an array with what I want to search for using the field name as the key and the value as the search value.

PHP Code:
$where = array("field1ID"=>34"field2ID"=>2"field3ID"=>65);

function 
whereClause$where, &$query ) {
  
$query .= " where ";
  
$moreThenOne false;
  foreach (
array_keys($where) as $key) {
    if(
$moreThenOne)
      
$query .= " and ";
    
$query .= $key " = \"" $where[$key] . "\" ";
    
$moreThenOne=true;
  }

__________________
toe_cutter is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
EnterpriseDB 2005 Wins Best Database Award at LinuxWorld technobard Code Newbie News 2 09-06-2005 07:41 PM
Help making a login script more secure superbeastie PHP 8 08-29-2005 07:48 AM
Database layout - opinions? Rafkin Everything SQL ( MySQL, MSSQL, DB2, Postgre, Oracle, etc...) 4 12-29-2004 07:36 AM
need help with making a script XGangstaX PHP 1 12-13-2004 06:37 AM
Methods of moving a database... DarkTwilkitri PHP 8 11-19-2003 05:02 AM


All times are GMT -8. The time now is 10:16 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting