Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using variables in a query

Status
Not open for further replies.

KempCGDR

Programmer
Jan 10, 2003
445
GB
Ok, this probably has a really obvious answer, but for the life of me, I can't it.

I have a page that I call with to parameters (race and table) and I need to use these in a query. I have this:

$query = "SELECT * FROM '$table' WHERE race='$race' ORDER BY itemName ASC"

the race='$race' bit works perfectly, I have it on a few other pages, but the SELECT * FROM '$table' fails. Can I actually use it like this?
 
This should work:
[tt]$query = "SELECT * FROM $table WHERE race='$race' ORDER BY itemName ASC";[/tt]
or
[tt]$query = "SELECT * FROM `$table` WHERE race='$race' ORDER BY itemName ASC";[/tt]
if the table name could be a reserved word in MySQL. //Daniel
 
Well.... the second suggestion of yours is identical to what I did that doesn't work, and I have tried the first one and that failed as well.

Is $table something that is already used by php for something?
 
The send one uses backticks, not quotes, which is the way you tell MySQL that it is a table or column. Are you sure that [tt]$table[/tt] and [tt]$race[/tt] contains valid information? //Daniel
 
Correction:
The send one uses backticks
should have read
The second one uses backticks. //Daniel
 
The problem seems to have solved itself, viewed the page with the code I was using before and now it works. It's on my local machine, so no configuration changes or anything. It just works. Damn inconsistancies.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top