How to separate a PDO named placeholder from rest of a string
I have a PDO prepared statement which looks like this:
$STH = $DBH->("CREATE TABLE :prefixbase (table_structure)");
:prefix should be replaced 开发者_StackOverflowwith userprefix_
. The resulting SQL should look like this: CREATE TABLE userprefix_base(table structure)
. How can I separate the placeholder from the rest of the string?
I guess you need to use regular expression or simple find-replace on your string, which after such processing can be used by PDO object as a proper SQL command.
Here is an example:
$subject = "CREATE TABLE :prefixbase(table_structure)";
$sql = preg_replace('/(:prefix)/', 'userprefix_', $subject);
// or
$sql = str_replace(':prefix', 'userprefix_', $subject);
P.S. I hope I understood your request correctly.
How about this:
$placeholder="userprefix_";
$stmt = $pdo->prepare("CREATE TABLE ".$placeholder."base (table_structure)");
Unless that prefix is supposed to change after having prepared the statement. That wouldn't work without preparing another statement.
精彩评论