1

I have this query

$prefix = 'de_';
    $sql = "INSERT INTO $prefix.request_products (created_at, request_id) VALUES (NOW(), :request_id)";

Which would be working, but php or PDO (or both) are writing the dot within the query, the table name is de_request_products but the query is build as de_.request_products.

If I just use a space between $prefix and the string PDO throws an error.

Is there any other possibility of how to concatenate the string and the variable within the query, or is the only way to build it like this:

$prefix = 'de_';
$table = 'request_products';
$concat = $prefix.$table;
$sql = INSERT INTO $concat ...
1
  • Try "INSERT INTO ".$prefix."request_products .... or "INSERT INTO {$prefix}request_products .... Commented Dec 27, 2014 at 23:00

2 Answers 2

2

Use ${variable} syntax:

$sql = "INSERT INTO ${prefix}request_products (created_at, request_id) VALUES (NOW(), :request_id)";
Sign up to request clarification or add additional context in comments.

Comments

1

The manual here: http://php.net/manual/pl/language.types.string.php says: "Enclose the variable name in curly braces to explicitly specify the end of the name." So this should make you happy:

$prefix = 'de_';
$sql = "INSERT INTO ${prefix}request_products (created_at, request_id) VALUES (NOW(), :request_id)";

Comments

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.