Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 03-27-2009, 08:06 PM MySQL WHERE selector
Novice Talker

Posts: 14
Name: Wade
Trades: 0
I have coded a php and mysql cms, the pages are loaded by
Code:
WHERE id=".$_GET['id']
It works great, but i would like to change it so the urlid is used to select a page so i changed it to
Code:
WHERE urlid=".$_GET['id']
. The page doesn't load due to the selector is meant to wrapped it a single quote.
Eg: When i go to view a page i got main.php?id=home but it doesnt work.
I tryed main.php?id='home' and it works fine.

Code:
http://www.w3schools.com/Sql/sql_where.asp
Is the correct way to do it.

Basicly all im trying to do is wrap the variable $_GET['id'] in single quotes.
I allready tryed
Code:
WHERE urlid=".'.$_GET['id'].';
but it don't work.

If you know how to wrap the variable in single quotes could you please help me out.

Thanks,
Wade
amphtech is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 03-27-2009, 08:10 PM Re: MySQL WHERE selector
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
I know of two ways.
One:
Code:
'SELECT * FROM table WHERE urlid=\'' . $_GET['id'] . '\'';
Two:
Use PDO (http://us2.php.net/pdo).
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 03-27-2009, 10:05 PM Re: MySQL WHERE selector
Novice Talker

Posts: 14
Name: Wade
Trades: 0
Thanks so much worked like a charm.
Thanks,
Wade
amphtech is offline
Reply With Quote
View Public Profile
 
Old 03-28-2009, 08:07 AM Re: MySQL WHERE selector
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Just to explain to you, the problem was the datatypes of your db columns.

id is referenced as an integer (or a variant: bigint, smallint) and can only contain integer numbers.
urlid is a varchar field.

A numeric field is referenced as is, without quotes
Code:
select * from tableX where intField=5;
If you want to look up a varchar value, you have to enclose the term into single quotes.
Code:
select * from tableX where strField='something';
If you try to make a query on an integer field with a varchar value, then the db will try to implicitly cast your varchar in an integer value
Code:
 select * from tableX where intField='5';
It works, but requires a little bit more work for the db.

But, the db won't cast a integer to varchar implicitely, so, when you do
Code:
 select * from tableX where strField=5;
the db raises an error because you try to compare a string value with an integer value.
What you should have written was
Code:
 select * from tableX where strField='5';
Thus your error.
__________________
Only a biker knows why a dog sticks his head out the window.

Last edited by tripy; 03-28-2009 at 08:09 AM..
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 03-29-2009, 12:02 AM Re: MySQL WHERE selector
Novice Talker

Posts: 14
Name: Wade
Trades: 0
Thanks,
Tripy for explaining my error
amphtech is offline
Reply With Quote
View Public Profile
 
Old 03-29-2009, 04:13 PM Re: MySQL WHERE selector
Novice Talker

Posts: 9
Name: Iman Mayes
Trades: 0
On a security note, this opens you up to a SQL Injection attack. Here is an article that explains this further and has a method for prevention.
__________________
Iman Mayes
Please login or register to view this content. Registration is FREE
imayes is offline
Reply With Quote
View Public Profile Visit imayes's homepage!
 
Old 03-30-2009, 01:09 AM Re: MySQL WHERE selector
Novice Talker

Posts: 14
Name: Wade
Trades: 0
So the first one example NullPointer posted will open it up to enable SQL Injection? If so how would I fix it?

Last edited by amphtech; 03-30-2009 at 01:12 AM..
amphtech is offline
Reply With Quote
View Public Profile
 
Old 03-30-2009, 01:15 AM Re: MySQL WHERE selector
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
Yes. User input should be validated and sanitized before use in an sql query. I forgot to mention that in my earlier post. You'll want to make sure that $_GET['id'] meets any constraints on IDs (string length, valid characters, etc). Also use mysql_real_escape string to escape special characters (http://us2.php.net/function.mysql-real-escape-string).
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Reply     « Reply to MySQL WHERE selector
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 1.62402 seconds with 12 queries