我正在尝试执行它,但它抛出 'SQLSTATE[IMSSP]: This function is not implemented by this driver.' 的错误

I am trying to execute it but it throws error of 'SQLSTATE[IMSSP]: This function is not implemented by this driver.'

我试过调用该函数,但它总是给我错误。 我的代码如下

$name = 'Devrishi Pandey';
$return_message = prepared_insert($pdo, 'my_table', ['name' => $name]);
function prepared_insert($pdo, $table, $data) {
    $keys = array_keys($data);
    $fields = implode(",", $keys);
    $placeholders = str_repeat('?,', count($keys) - 1) . '?';
    try{
        $sql = "INSERT INTO $table ($fields) VALUES ($placeholders)";
        $pdo->prepare($sql);
        $pdo->execute($data);
        return $pdo->lastInsertId();
    } catch(PDOException $e){
        return $sql . "<br>" . $e->getMessage();
    }
}

execute 函数用于 PDOStatement,而不是连接。您需要将准备好的语句分配给一个变量,然后在该变量上执行。

$name = 'Devrishi Pandey';
$return_message = prepared_insert($pdo, 'my_table', ['name' => $name]);
function prepared_insert($pdo, $table, $data) {
    $keys = array_keys($data);
    $fields = implode(",", $keys);
    $placeholders = str_repeat('?,', count($keys) - 1) . '?';
    try{
        $sql = "INSERT INTO $table ($fields) VALUES ($placeholders)";
        $stmt = $pdo->prepare($sql);
        $stmt->execute($data);
        return $pdo->lastInsertId();
    } catch(PDOException $e){
        return $sql . "<br>" . $e->getMessage();
    }
}