开发者

How to separate a PDO named placeholder from rest of a string

开发者 https://www.devze.com 2023-03-12 10:31 出处:网络
I have a PDO prepared statement which looks like this: $STH = $DBH->(\"CREATE TABLE :prefixbase (table_structure)\");

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消