問題:
如何取代過時的mysql_* 函數PDO 和準備好的語句來安全地儲存和檢索資料庫中的資料?
答案:
$hostname = '*host*'; $username = '*user*'; $password = '*pass*'; $database = '*database*'; $dbh = new PDO("mysql:host=$hostname;dbname=$database", $username, $password);
$username = $_POST['username']; $email = $_POST['email']; $stmt = $dbh->prepare("INSERT INTO `users` (username, email) VALUES (?, ?)"); $stmt->bindParam(1, $username, PDO::PARAM_STR); $stmt->bindParam(2, $email, PDO::PARAM_STR); $stmt->execute();
$stmt->bindParam(1, $username, PDO::PARAM_STR, 255);
$user_id = $_GET['id']; $stmt = $dbh->prepare("SELECT * FROM `users` WHERE `id` = ?"); $stmt->bindParam(1, $user_id, PDO::PARAM_INT);
以上是如何用 PDO 和準備好的語句取代過時的「mysql_*」函數以實現安全的資料庫互動?的詳細內容。更多資訊請關注PHP中文網其他相關文章!