<?php
$host = ‘127.0.0.1‘;
$user = ‘root‘;
$pwd = ‘123456‘;
$dbname = ‘test‘;
// sql
$sql = "delete from user where id=1;";
try {
// 连接数据库
$conn = new PDO(‘mysql:host=‘ . $host . ‘;dbname=‘ . $dbname . ‘‘, $user, $pwd);
// 设置 PDO 错误模式为异常
$conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$conn->exec(‘set names "utf8"‘);
// 使用 exec() ,因为没有结果返回
$affected = $conn->exec($sql);
if ($affected !== false) {
echo 1;
return;
} else {
echo 2;
$conn = null;
return;
}
} catch (PDOException $e) {
// echo $sql . "<br>" . $e->getMessage();
echo 0;
$conn = null;
return;
}
pdo 执行sql