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

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:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old September 11th, 2003, 04:15 PM
tankdogg tankdogg is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Location: Ohio
Posts: 7 tankdogg User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Angry need help!! using 2 php variables im mysql_query

I'm going nuts here. Ok this is what I'm trying to do. I have a page where someone picks from 2 different categories to sort data by. say the topic is food & they pick fruit & then subcategory apples. I'm sending this info to the results page as variables $selection & $type. now fruit would be one of my fields in my table & my code looks like this:

// Request the text
$result = mysql_query("SELECT * FROM test WHERE @selection=@type");
if (!$result) {
echo("<P>Error performing query: " .
mysql_error() . "</P>");
exit();
}

What am I doing wrong. Can I not use 2 variables like that in the select statement? I've tried many variations on this same basic idea. If anyone could please help me to fix this or has another suggestion to get these results I would be eternally grateful. I would be happy to clarify anything or provide any further info needed. sorry if this is stupid but I'm very new to both php & mysql.

Last edited by tankdogg : September 11th, 2003 at 04:29 PM.

Reply With Quote
  #2  
Old September 11th, 2003, 06:19 PM
FrankieShakes FrankieShakes is offline
Frank The Tank!
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Jun 2002
Location: Toronto, Canada
Posts: 1,246 FrankieShakes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
Send a message via ICQ to FrankieShakes Send a message via MSN to FrankieShakes
tankdogg,

Why are you using @ symbols instead of $ symbols for your variables?

Also, depending on your version of PHP, you may need to reference your variables using the superglobal arrays:

ie: $_POST['selection'] or $_GET['selection']

Hope that helps...
__________________
____________________________________________
Developer Shed Weekly Writer | DevArticles Forum Moderator
Build Your Own KlipFolio Klip With PHP
FrankManno.com - Under Construction
Design Interactive Group - Under Construction

Reply With Quote
  #3  
Old September 11th, 2003, 06:22 PM
_rainbow_ _rainbow_ is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Location: Lj, Slovenia
Posts: 27 _rainbow_ User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 m 28 sec
Reputation Power: 0
Why the @? Try it like this:

PHP Code:
 $result mysql_query("SELECT * FROM test WHERE $selection='$type'"); 


The variable $type is between single quotes (I find it hard to see).

Reply With Quote
  #4  
Old September 11th, 2003, 06:51 PM
tankdogg tankdogg is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Location: Ohio
Posts: 7 tankdogg User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I've tried that also can you use 2 variables like that? what happens is I get an error at the =

Reply With Quote
  #5  
Old September 11th, 2003, 07:03 PM
tankdogg tankdogg is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Location: Ohio
Posts: 7 tankdogg User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0

PHP Code:
 Error performing queryYou have an error in your SQL syntax near '=''' at line 1 

Last edited by tankdogg : September 11th, 2003 at 07:10 PM.

Reply With Quote
  #6  
Old September 11th, 2003, 07:18 PM
FrankieShakes FrankieShakes is offline
Frank The Tank!
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Jun 2002
Location: Toronto, Canada
Posts: 1,246 FrankieShakes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
Send a message via ICQ to FrankieShakes Send a message via MSN to FrankieShakes
Try this...

Store your query into a variable first...

PHP Code:
 $sql "SELECT * FROM test WHERE $selection='$type'";

echo 
"SQL Query: " $sql "\n"


That should display the exact query getting sent to the DB...

Let me know what's displayed.

Reply With Quote
  #7  
Old September 11th, 2003, 11:33 PM
tankdogg tankdogg is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Location: Ohio
Posts: 7 tankdogg User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
this is what I get

SQL Query: SELECT * FROM test WHERE =''

Reply With Quote
  #8  
Old September 12th, 2003, 06:58 AM
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
Your variables aren't being set. Where are they coming from? Are they form results? If so, is register_globals turned on in your PHP config? Whether or not they are, it'd be a good idea to refer to variables within scope, so, for example, a form variable named "type" you would invoke as $_POST["type"] or $_GET["type"] depending on which method you used to submit the form.

Try setting values for the two variables above your statement and see what is echoed. If it works, then you've got a problem with how you're initially getting values into your variables.

Reply With Quote
  #9  
Old September 12th, 2003, 08:18 AM
tankdogg tankdogg is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Location: Ohio
Posts: 7 tankdogg User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
thanks, setting the variables manually did work & gave me the correct results. What I was doing to test it was setting them via the url. in aplication they will be set via menu selections, which I now have set using links ie.
....<a href="results.php?selection=whatever&type=whatever">

any suggestions of a better way of doing this? because its not transferring the values over.

Reply With Quote
  #10  
Old September 12th, 2003, 09:41 AM
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
It's probably simply the case that register_globals is turned off. In your code, replace $selection and $type with $_GET[selection] and $_GET[type] and see how you fare with that. Also, you might want to consider wrapping each of those variables in addslashes() to help safeguard against an SQL injection attack (somebody hacking the URL and setting $type to "test; DROP TABLE users" for example). Your code would need to look like:

PHP Code:
 $sql "SELECT * FROM test WHERE " addslashes($_GET["selection"]) . " ='" addslashes($_GET["type"]) . "'"

Reply With Quote
  #11  
Old September 12th, 2003, 03:46 PM
tankdogg tankdogg is offline
Junior Member
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Location: Ohio
Posts: 7 tankdogg User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Talking

thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you, thank you

It now works perfectly. dhouston you are my new hero. thank you to everyone you are all great.

and incase I forgot. THANK YOU.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesGeneral SQL Development > can anyone please help me???


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 | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway