1

When trying to use

$SomeTable = "SomeTable";
INSERT INTO "' . $SomeTable . '" SET ... 

instead of

INSERT INTO SomeTable SET ...

I am unsuccessful. Why am I unable to use a php variable to call a database name?

5
  • 1
    You need to wrap table names (and column names) in backticks, not single or double quotes. Commented Sep 13, 2013 at 15:56
  • And you should really be using prepared statements rather than just concatenating strings. Commented Sep 13, 2013 at 15:57
  • @Chris Sadly you can't use placeholders for table names. This is a massive oversight. Commented Sep 13, 2013 at 15:59
  • @tadman Really!? I would have thought it would be the same principle? Wow. Learn something new every day. Commented Sep 13, 2013 at 16:00
  • PDO really should do it, but for some reason it's never gained this capability. Data placeholders are always escaped as strings, but table names use a different escaping method. Commented Sep 13, 2013 at 16:06

1 Answer 1

2

You would need to use something like this:

$SomeTable = "SomeTable";
$query = 'INSERT INTO `' . $SomeTable . '` SET ... ';

Which uses the ` character instead of quotes

Sign up to request clarification or add additional context in comments.

2 Comments

why are the backticks necessary there?
I believe its to help distinguish it from SQL statements such as WHERE, FROM, etc. I didn't find anything official in my brief google

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.