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 04-10-2005, 05:53 AM   #1 (permalink)
Sonny1973
Registered User
 
Join Date: Mar 2005
Posts: 7
Sonny1973 is on a distinguished road
Question Query string

i'd like to know how you would structure the following query for multiple selectnames (which are all contained in the search form, via drop down menu options):

$query = mysql_query("SELECT * FROM tablename WHERE selectname LIKE '%$search%' LIMIT 0, 50"

also, i'd like to get rid of the Search box in the search form. so in other words i don't want to reference a search value, '%$search%', at all. thanks for any help in advance.
Sonny1973 is offline   Reply With Quote
Old 04-10-2005, 09:12 AM   #2 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,470
sde is on a distinguished road
you are saying you want the query to support multiple names, but a drop down menu only allows you to select one name. is this from multiple select boxes? or 1, multi-select box.

if you are controlling the drop down options, then you can put exact names in there. this will eleminate the need for the wild cards " % " around the search variable.

since you mention multiple names, for now i will forget about how you are getting them into the script and i'll just use an array. below are 2 methods which you could search for multiple names.

Code:
SELECT * FROM tablename WHERE selectname IN ('Joe Smith','Mike Jones')
Code:
SELECT * FROM tablename WHERE selectname='Joe Smith' OR selectname='Mike Jones'
with PHP, we could make these queries dynamically if the selected names were in an array. Here's an example:
PHP Code:
<?
$names 
= array('Joe Smith','Mike Jones');

// start sql
$sql "SELECT * FROM tablename WHERE selectname IN ";

for(
$i=0;$i<count($names);$i++){
  
$sql .= $names[$i];

  
// create a comma if it's not the last item in the array
  
if($i<$count)
    
$sql .= ",";
}

// finish sql string
$sql .= ")";

// query db
$result mysql_query($sql);
?>
you would use the same logic if you opted to use the other sql query too. you are just building a string from an array.
__________________
Mike
sde is offline   Reply With Quote
Old 04-11-2005, 12:07 PM   #3 (permalink)
Sonny1973
Registered User
 
Join Date: Mar 2005
Posts: 7
Sonny1973 is on a distinguished road
actually, it is from multiple select boxes. and i appreciate the advice.
Sonny1973 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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help for another program Androto Standard C, C++ 54 10-15-2004 07:21 AM
sorting objects arrays by object properties sde Java 28 08-05-2004 05:51 AM
From C to Java HighterDK Java 11 07-13-2004 07:15 PM


All times are GMT -8. The time now is 07:31 PM.


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