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
Go Back   Code Forums > Application and Web Development > Everything SQL ( MySQL, MSSQL, DB2, Postgre, Oracle, etc...)
User Name
Password

Reply
 
LinkBack Thread Tools Display Modes
Old 03-07-2008, 05:52 PM   #1 (permalink)
falsepride
Regular Contributor
 
Join Date: Oct 2004
Posts: 192
falsepride is on a distinguished road
query wildcards

i need to query a bunch of entries from a table from variable columns with a wild card before and after the search term. thats vague i know. so heres my query, but i don't think its a valid query.

Code:
mysql_query("SELECT * FROM livestock WHERE" . $_GET['zone'] . "LIKE '%" . $_GET['name'] . "%'");
__________________
falsepride is offline   Reply With Quote
Old 03-07-2008, 08:32 PM   #2 (permalink)
bdl
Senior Contributor
 
Join Date: May 2002
Location: vta.ca.usa
Posts: 555
bdl is on a distinguished road
Is there an error returned from the query? Take a look at the string MySQL sees:

(as an example, let's say $_GET['zone'] is 'column1' and $_GET['name'] is 'Bob')

Code:
SELECT * FROM livestock WHEREcolumn1LIKE '%Bob%'
Doesn't look right, does it? You're not formatting the string properly; make sure you leave some space between the WHERE clause and the column name, e.g.
Code:
SELECT * FROM livestock WHERE column1 LIKE '%Bob%'
Now, that's an absolutely valid query, but is it what you want? Refer to the MySQL manual page for the LIKE operator and basic pattern matching. If you need to control a more specific pattern, look into regular expressions.

In addition, it's important to be aware that using unvalidated, unescaped GET (or POST) data straight in your query is a Very Bad Idea, especially if you're using 'dynamic column names'. I'd suggest storing a list of allowed columns in an array and only allowing those columns to be accessed.
__________________
bdl is offline   Reply With Quote
Old 03-08-2008, 02:47 PM   #3 (permalink)
falsepride
Regular Contributor
 
Join Date: Oct 2004
Posts: 192
falsepride is on a distinguished road
using $_GET directly in my query won't be a problem in this case. this page is only going to be accessed from a javascript xmlhttprequest. the javascript will limit what column names get put into the queries.

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''%tri%'' at line 1

is the error i got from the query
__________________
falsepride is offline   Reply With Quote
Old 03-08-2008, 02:58 PM   #4 (permalink)
falsepride
Regular Contributor
 
Join Date: Oct 2004
Posts: 192
falsepride is on a distinguished road
i exceed my limit to edit my last post, so yea bdl you were right i needed to add some space to my query
Code:
$result = mysql_query("SELECT * FROM livestock WHERE " . $_GET['zone'] . " LIKE '%" . $_GET['name'] . "%'");
__________________
falsepride is offline   Reply With Quote
Old 03-08-2008, 05:59 PM   #5 (permalink)
bdl
Senior Contributor
 
Join Date: May 2002
Location: vta.ca.usa
Posts: 555
bdl is on a distinguished road
Quote:
Originally Posted by falsepride View Post
using $_GET directly in my query won't be a problem in this case. this page is only going to be accessed from a javascript xmlhttprequest. the javascript will limit what column names get put into the queries.
I hate to poke holes in your logic, but one of the easiest things a malicious user can do is simply view your page source and understand how the script accepts values, bypass JavaScript altogether and create their own form to send data directly to your PHP script. It's also as easy as a couple of mouse clicks to disable JavaScript in the browser.

You should always double up on filtering data; using JavaScript to do it up front is great, it can save time on multiple page requests kicking back to the user until they give you what you expect. BUT, if the user disables it (either because they look at it from the viewpoint of their own security, or do it specifically to bypass your checks) you have to have a backup in the server side code to perform the same task. At the most, all that happens is the data is checked twice.
__________________
bdl is offline   Reply With Quote
Old 03-08-2008, 06:15 PM   #6 (permalink)
falsepride
Regular Contributor
 
Join Date: Oct 2004
Posts: 192
falsepride is on a distinguished road
i only meant it won't be a problem for the dynamic column names. i am going to start a new thread actually, talking about security. and buffering variables before they get put into sql queries. speaking of, i think i might as well do that now.
__________________
falsepride is offline   Reply With Quote
Reply


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

vB 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
Do Some Calculations on Query Results transfield PHP 9 03-06-2007 12:07 PM
Re-query The Query Results transfield PHP 14 02-19-2007 12:55 PM
socket based CS server query Redline PHP 11 06-18-2006 05:20 PM
a query type of questiton sde Program Design and Methods 6 08-21-2003 04:55 PM
foreach() while() for() or new mysql query? nemesis PHP 6 06-10-2003 06:57 PM


All times are GMT -8. The time now is 08:17 AM.


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





Copyright © 2000-2006, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
Open Circle