PHP/MySQL - Proper syntax for using alphabetical variables in a query?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
PHP/MySQL - Proper syntax for using alphabetical variables in a query?
I'm trying to use variables for a table and column name in a query, like:
Code:
$Table = "Products";
$Column = "Price";
$result = mysql_query("SELECT * FROM '$Table' ORDER BY '$Column'");
while($row = mysql_fetch_array($result))
{
// Stuff.
}
It won't work, I get the ubiquitous error:
Code:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in...
I am able to use variables for numerical values without any problems (don't even need to use single quotation marks), but I can't figure-out how to make it so that I can use alphabetical values in queries such as the example I posted. Could you please show me the way?
Yeah, but notice you've removed the single-quote marks around the column names.
In many languages eg Shell, Perl, PHP, etc single-quote means DON'T interpolate the (apparent) value here, use this string as is.
You could use double-quotes, which DOES do interpolation, or not use quotes (as you have here).
Thanks for the info. Weird thing is, I had indeed tried without any quotation marks at all too, and it wouldn't work either. Not sure if the error was the same, though. But once I fixed a couple problems with $row['blah'] things within the curly braces it worked (as it appears in my second post), so my guess is it had something to do with the curly brace content. Not sure if that makes sense, though. =/
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.