LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-09-2008, 10:07 PM   #1
win32sux
Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Rep: Reputation: 371Reputation: 371Reputation: 371Reputation: 371
Question 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?
 
Old 03-09-2008, 10:38 PM   #2
win32sux
Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Original Poster
Rep: Reputation: 371Reputation: 371Reputation: 371Reputation: 371
Weird, I fixed some errors within the curly braces (// Stuff.) and it now works fine like:
Code:
$Table = "Products";
$Column = "Price";

$result = mysql_query("SELECT * FROM $Table ORDER BY $Column");
while($row = mysql_fetch_array($result))
  {
    // Stuff.
  }

Last edited by win32sux; 03-09-2008 at 10:43 PM.
 
Old 03-10-2008, 02:16 AM   #3
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,226

Rep: Reputation: 2023Reputation: 2023Reputation: 2023Reputation: 2023Reputation: 2023Reputation: 2023Reputation: 2023Reputation: 2023Reputation: 2023Reputation: 2023Reputation: 2023
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).
 
Old 03-10-2008, 06:37 AM   #4
win32sux
Guru
 
Registered: Jul 2003
Location: Los Angeles
Distribution: Ubuntu
Posts: 9,870

Original Poster
Rep: Reputation: 371Reputation: 371Reputation: 371Reputation: 371
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. =/
 
  


Reply


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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Proper way of removing Apache, PHP and MySQL Basel Slackware 1 01-29-2006 04:34 AM
php/,mysql problem: can't query JJX Linux - General 4 01-06-2005 05:10 PM
php: Why Can't I Query Mysql DB?? flamesrock Programming 7 11-16-2004 12:36 AM
Adding javascript variables to a PHP query Elijah Programming 7 11-07-2004 07:28 PM
A Simple MySQL Query Question (I have Bad Syntax) bpk General 1 12-02-2003 09:04 PM


All times are GMT -5. The time now is 03:32 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration