1. mysqli – 面向对象
$conn = new mysqli($servername,$username,$password);
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
echo "连接成功";
$conn -> close();
2. mysqli – 面向过程
$conn = mysqli_connect($servername,$username,$password);
if(mysqli_connect_errno($conn)) {
die("Connect failed: " . $conn->connect_error());
}
echo "连接成功";
mysqli_close($conn);
3. pdo – 面向对象
try {
//初始化一个PDO对象
$dbh = new PDO("$dbms:host=$host;dbname=$dbName",$username,$password);
echo "连接成功";
} catch (PDOException $e) {
die("Error!: " . $e->getMessage());
}
$dbh = null;
4. mysql – 面向过程
$conn = mysql_connect($servername,$username,$password);
if(!$conn) {
die('Could not connect: ' . mysql_error());
}
echo "连接成功";
mysql_close($conn);