What is the proper way to quote a variable used in a MySQL query?

Experts,

I made the mistake of naming some of my columns in my table with only numbers.

This is causing problems when trying to update the values in my table. I understand that I need to quote the column name but, I am going in circles.

Here's an example...the value of $template_id = 2

Here's my PHP query:

$update_template_matrix = "UPDATE db12.tblname SET ".$template_id."=1 WHERE id='".$id."'";

How do I quote the $template_id variable (which is a single digit) so that I can use it properly in this query?
evibesmusicAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
In a double-quoted string, you do not normally need to '.' dot-concatenate variables.  Back-ticks are used in MySQL to quote column, table, and database names.  But seriously, you should go give your columns proper names because this problem will continue to cause problems until you do.

$update_template_matrix = "UPDATE db12.tblname SET `$template_id`=1 WHERE id='$id' ";

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
shishir_sriCommented:
Hey,

You need to use this character: `

So your query will become:
$update_template_matrix = "UPDATE db12.tblname SET `".$template_id."` =1 WHERE id='".$id."'";

Open in new window


You can also do this:
$update_template_matrix = "UPDATE db12.tblname SET `$template_id` =1 WHERE id='$id'";

Open in new window


Let me know if either of the two don't work.

best,
Shishir S.
0
evibesmusicAuthor Commented:
@DaveBaldwin:

What keyboard strokes do you use to produce the back-tick?
0
Newly released Acronis True Image 2019

In announcing the release of the 15th Anniversary Edition of Acronis True Image 2019, the company revealed that its artificial intelligence-based anti-ransomware technology – stopped more than 200,000 ransomware attacks on 150,000 customers last year.

Dave BaldwinFixer of ProblemsCommented:
It's in the upper left corner of the US keyboard on the same key as ~ which is usually under the ESC key.
0
evibesmusicAuthor Commented:
@DaveBaldwin:

Ahhh...yes...I've seen it many a times just never had a use for it until now.

Silly me for using a number-only naming convention for my table names...nothing like backtracking through a ton of code to fix this issue.

Thanks @DaveBaldwin!
0
evibesmusicAuthor Commented:
Thanks!
0
Dave BaldwinFixer of ProblemsCommented:
You're welcome.  Note that many languages like javascript do not allow the first character of a variable name to be a number.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.