Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2009-05-03
7
Medium Priority
?
358 Views
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.
0
Comment
Question by:alicia1234
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 143

Accepted Solution

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

Expert Comment

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

Open in new window

0
 

Author Comment

by:alicia1234
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.
Thanks!

0
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 
LVL 143

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.
0
 

Author Comment

by:alicia1234
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
0
 
LVL 143

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

0
 

Author Comment

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

0

Featured Post

Tutorial: Introduction to Managing a Linux Server

In this tutorial on systemd, we will explore:
-OS/Distro Adoption
-chkconfig and Other Legacy Commands
-Summary and Key Commands

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

730 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