MySQLi 和 PDO 连接 MySQL 实例
实例 (MySQLi - 面向过程)
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// 创建连接
$conn = mysqli_connect($servername,
$username, $password);
// 检测连接
if (!$conn) {
die("Connection
failed: " . mysqli_connect_error());
}
echo "连接成功";
?>
实例 (PDO)
<?php
$servername = "localhost";
$username = "username";
$password = "password";
try {
$conn = new PDO("mysql:host=$servername;dbname=myDB",
$username, $password);
echo "连接成功";
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
文章来自 www.dg-haiyue.cn