How do I use PHP variables as field names in a MySQL query?

Posted on 2009-05-03
Last Modified: 2012-05-06
For example:

{$fieldname = "mygreatfield"}

My current query is:
$query = "SELECT mygreatfield FROM mytable WHERE id=3";

In the query, what is the syntax for replacing "mygreatfield" with the value in $fieldname.

Keep in mind that the $query is already inside <?php  ... ?> tags, so trying to just insert an echo doesn't look right to me, so I didn't even attempt it.

High points on this cause I'm working on a deadline and need an answer fast.
Question by:alicia1234
  • 4
  • 3
LVL 142

Accepted Solution

Guy Hengel [angelIII / a3] earned 500 total points
ID: 24291083
$query = "SELECT {$fieldname} FROM mytable WHERE id=3";
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24291088
$query = "SELECT `" . $fieldname . "` FROM mytable WHERE id=3";


$query = "SELECT `{$fieldname}` FROM mytable WHERE id=3";

Open in new window


Author Comment

ID: 24291320
How nice that it's that simple. I should have realized it because I do the same thing for the values. For some reason, I guess I just thought that field names would work differently.

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24291330
glad I could help.
note that you are aware of the difference of the quotes (') for the string values vs the backticks (`) for the table/column names.

Author Comment

ID: 24291347
Should this same syntax work for POST vars? Like this:

$lastfname = "staples"
if ( isset($_POST['{$lastfname}']) ....

because it doesn't see to be working
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24291357
you put {} where you don't need them

if ( isset($_POST[$lastfname]) ....

Open in new window


Author Comment

ID: 24291370
Thanks! Again ... ;-)


Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now