将代码从mysqli更改为PDO可以通过以下步骤完成:
$dsn = "mysql:host=localhost;dbname=mydatabase";
$username = "myusername";
$password = "mypassword";
try {
$pdo = new PDO($dsn, $username, $password);
} catch (PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
$stmt = $pdo->prepare("SELECT * FROM mytable WHERE id = :id");
$stmt->bindParam(':id', $id);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
$stmt = $pdo->prepare("INSERT INTO mytable (name, email) VALUES (:name, :email)");
$stmt->bindParam(':name', $name);
$stmt->bindParam(':email', $email);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$pdo = null;
通过以上步骤,你可以将代码从mysqli更改为PDO。PDO提供了更多的功能和更好的安全性,同时也更加灵活和易于使用。
领取专属 10元无门槛券
手把手带您无忧上云