PDO
样品用法:
<?php
try {
// config
$dsn = 'mysql:dbname=testdb;host=127.0.0.1;charset=utf8';
$username = 'root';
$password = '';
$options = array(
PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
); // You should always use these options
// conncect
$pdo = new PDO($dsn, $username, $password, $options);
// check posted values
if (
!isset($_POST['id'], $_POST['message']) ||
!is_string($_POST['id']) ||
!is_string($_POST['message'])
) {
throw new RuntimeException('invalid parameters');
}
// SQL execution
$stmt = $pdo->prepare('UPDATE forum_reactions SET message = ? WHERE id = ?');
$stmt->execute(array($_POST['message'], $_POST['id']));
// check result
if ($stmt->rowCount()) {
echo 'successfully updated';
} else {
echo 'specified ID not found.';
}
} catch (Exception $e) {
echo $e->getMessage();
}