General Programming Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingGeneral Programming Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old June 27th, 2003, 03:43 AM
thecharking thecharking is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 187 thecharking User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Send a message via AIM to thecharking
searching from the url

what is the best way to search through a database through variables from the url? I was looking at making a complex if {} else {} system, but it is daunting. Iw as wondering if anyone had any shortcuts they knew of, is there a function of php I don't know of maybe that would help me out? any ehlp is much appreciated thanks.
__________________
hey it's the CHARKING

Reply With Quote
  #2  
Old June 27th, 2003, 06:12 AM
digitallysmooth digitallysmooth is offline
you know how we do
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jun 2002
Posts: 788 digitallysmooth User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 34 m 21 sec
Reputation Power: 7
Please clarify this request. I'm not sure what you are asking.
__________________
__________________________________________________ _
Wil Moore III, MCP | Integrations Specialist | Senior Consultant
Are You Listed...? | DigitallySmooth Inc.

Reply With Quote
  #3  
Old June 27th, 2003, 08:16 AM
devilFish devilFish is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 11 devilFish User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Yes, I am not really sure as to what you mean by this. If you mean just searching for alets say a news article and in the url id=2 the you would do:

Quote:
SELECT * FROM news_table WHERE news_id = $id


If this is no what you mean then you will have to be more specific and give an example.

Reply With Quote
  #4  
Old June 27th, 2003, 09:37 AM
digitallysmooth digitallysmooth is offline
you know how we do
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jun 2002
Posts: 788 digitallysmooth User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 34 m 21 sec
Reputation Power: 7
Don't Do This:
Quote:
SELECT * FROM news_table WHERE news_id = $id

Do This:
PHP Code:
 SELECT FROM news_table WHERE news_id = ${_GET['id']} 


This is because register_globals are most likely turned off in your later versions of PHP.
This can cause all sorts of portability problems.

Reply With Quote
  #5  
Old June 27th, 2003, 11:01 AM
fakker fakker is offline
The calm b4 the storm
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Location: Manchester, UK
Posts: 404 fakker User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via Yahoo to fakker
just on another note regarding search queries... I log every search item which people enter to search my site... on in particular stood out... it was the following:

; delete from members; delete from customers;

my search SQL statement was simply

SELECT * FROM table WHERE word LIKE search;

I think that if I hadnt had the extra ";" at the end, the user who entered the search above (delete) could have deleted the tables..!!? (any input on that would be appreciated!!)

Would this have worked if I didnt have the ";" ??

To answer your question, thecharking, I would simply write an SQL statement which queries the DB using all the search terms. You could do it all in one statement, then simply output the results... that shouldnt really need too many IF statements...

IF no results... echo "no results" else <show results>!!
__________________
Matt 'Fakker' Facer

mattfacer.com

Reply With Quote
  #6  
Old June 27th, 2003, 11:09 AM
devilFish devilFish is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 11 devilFish User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
Originally posted by laidbak
Don't Do This:

Do This:
PHP Code:
 SELECT FROM news_table WHERE news_id = ${_GET['id']} 


This is because register_globals are most likely turned off in your later versions of PHP.
This can cause all sorts of portability problems.


I realized that when I posted it, I posted that method because he sounds like a beginner and didnt want to confuse him. Also, it is unlikely that this application will be used as a large scale app, therefor portability is not as great a concern as it would be if it was a larger application, and he wasnt a beginner.

And before you reply to this one, take note of the actual words I have used such as "unlikey", not impossible.

Reply With Quote
  #7  
Old June 27th, 2003, 11:42 AM
digitallysmooth digitallysmooth is offline
you know how we do
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jun 2002
Posts: 788 digitallysmooth User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 34 m 21 sec
Reputation Power: 7
Point taken devilfish... he may have been a beginner.
I agree.

The only issue I see with not telling a user to use the $_GET style is that they will eventually see information telling them to use one or the other, then they are going to be really confuse and end up having to do the research anyway.

Better to learn it early then have to wait and struggle through it.

Reply With Quote
  #8  
Old June 27th, 2003, 01:49 PM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
Could have, fakker. That's why you should always validate your data. If you're doing an query by id passed through a form field or at the query string, then run intval() on the results to verify that somebody's not sending you something like

PHP Code:
 0delete from customers;


in which case your query might be well-formed and some damage could be done. If somebody passed that value to your variable and you validated it to make sure you had an integer, then their malicious attempt would be foiled.

Any time I'm reading data for insert or update, I make sure I place my quotes manually and run addslashes() to turn any value sent into a field value rather than a statement. There are probably more and better ways to validate user input, but these're a good start.

Reply With Quote
  #9  
Old June 28th, 2003, 03:00 AM
thecharking thecharking is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 187 thecharking User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Send a message via AIM to thecharking
thanks

alright so, sorry i wasn't clear, and haven't been able to respond to all your responses (thanks btw). Alright so I want to simply allow a user to search from a page for certain users, by name, age, whatever info is in the db... and see I can do this for each thing by passing the variables through the url (and I always change the url variable into a page variable with $_GET['']). But say that someone wants to find a user based on age and name. Well what would be the best way to do this? because if I try querying the db according to which things are passed, by using if statements that means I have to check for each one in various ways... is there a way to query a db by say, trying select * from users where userid = $userid and/or blah = $blah... anyway is there a way to do that that seems fast. if not, I would like to know how to search according to many variables at once.

thanks guys

Reply With Quote
  #10  
Old June 28th, 2003, 10:51 AM
Taelo Taelo is offline
5B's
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Location: PC, FL
Posts: 366 Taelo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 30 m 59 sec
Reputation Power: 7
well,...this can get complex,....you need to provide for every option you plan to search by...

here is how I specify the sort method and the direction of the sort.
PHP Code:
switch( @$_GET['sort'] )
{
    case 
"fName":
    
$sortField "fName";
    break;
    
    case 
"lName":
    
$sortField "lName";
    break;
    
    case 
"city":
    
$sortField "city";
    break;
    
    case 
"state":
    
$sortField "state";
    break;
    
    case 
"zip":
    
$sortField "zip";
    break;
    
    default:
    
$sortField "zip";
    break;
}

switch( @
$_GET['dir'] )
{
    case 
"asc":
    
$dir "asc";
    break;
    
    case 
"desc":
    
$dir "desc";
    break;
    
    default:
    
$dir "asc";
    break;



you really need to step back and think about this,..because you need to build this so that you only query the fields that are present. that will speed your search up considerably.
__________________
-- Jason

Reply With Quote
  #11  
Old June 28th, 2003, 11:36 AM
digitallysmooth digitallysmooth is offline
you know how we do
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jun 2002
Posts: 788 digitallysmooth User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 34 m 21 sec
Reputation Power: 7
There are lots of little details that you must consider.

1. What your SQL will look like if the user searches by their word in full or just with whatever they type in return anything with that word in it... take a look at google's advanced search: http://www.google.com/advanced_search

You will notice they have 4 options at the top. Each of these produces different SQL in the WHERE portion.

For instance, if I wanted to do a search on FirstName and LastName, sql would look something like this:
PHP Code:
 SELECT f_namel_name FROM athlete WHERE l_name LIKE '%oo%' OR f_name LIK
'%dd%'
Notice the "%" placement. This would grab any firstname or lastname with the specified criteria anywhere in that field.
For a full word match you would just do WHERE field = word. You can still use the LIKE pattern if you want but it does slow you down a little bit.

2. Next you just need to rember you will need to build your query up from whatever is posted to the search script. This means you have to code in conditions for every field selected to search on. Just remember that you can treat this part of your SQL as an array and just split it... instead of the usual comma as a delimiter use the SQL constructs to tie them all together again.

Reply With Quote
  #12  
Old June 28th, 2003, 03:59 PM
thecharking thecharking is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 187 thecharking User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
Send a message via AIM to thecharking
i was afraid it would be complex. okay so i guess I'll just have think about it a lot and do it. I was hoping there was a shortcut. Oh well this ought to be fun. I'll tell you if I get more problems, and I'm sure I will....

Reply With Quote
  #13  
Old June 29th, 2003, 12:14 AM
Taelo Taelo is offline
5B's
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Location: PC, FL
Posts: 366 Taelo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 30 m 59 sec
Reputation Power: 7
the best way to learn is to do it,.....you will start out small then think to yourself,..."hey,...if I did this and this,...it would be sooo much more flexible",....then you will upgrade your code,...and go through this process forever

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingGeneral Programming Help > searching from the url


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |