实例 (PDO)
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDBPDO";
try {
conn=newPDO("mysql:host=conn = new PDO("mysql:host=servername;dbname=dbname",dbname", username, $password);
// 设置 PDO 错误模式,用于抛出异常
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// 使用 sql 创建数据表
$sql = "CREATE TABLE MyGuests (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP
)";
// 使用 exec() ,没有结果返回
conn−>exec(conn->exec(sql);
echo "数据表 MyGuests 创建成功";
}
catch(PDOException $e)
{
echo sql."".sql . " " . e->getMessage();
}
$conn = null;
?>