尝试使用sprintf时,为什么会出现“基表或未找到视图”?

时间:2018-05-30 22:48:24

标签: php

$sql = sprintf(
    'insert into $s ($s) values ($s)',
    $table,
    implode(', ', array_keys($parameters)),
    ':' . implode(', :', array_keys($parameters))
);
try {
    $statement = $this->pdo->prepare($sql);
    $statement->execute($parameters);
} catch (Exception $e) {
    die($e->getMessage());
}

其他文件:

$query->test('todos', [
    'description' => $_POST['name'] . ' ' . $_POST['lastName'],
    'completed' => isset($_POST['finished'])
]);

这是奇怪的原因如果我手动完成而不是完美的

 $sql = "insert into $table (description, completed) values (:description, :completed)";

但是当我使用sprintf和implode时,它给出了一个错误,说“找不到基表或视图”,为什么会有任何想法?

1 个答案:

答案 0 :(得分:3)

因为您正在使用$PHP Documentation For sprintf

$sql = sprintf(
    'insert into %s (%s) values (%s)',
    $table,
    implode(', ', array_keys($parameters)),
    ':' . implode(', :', array_keys($parameters))
);

https://3v4l.org/2oEsT