PHP连接Mysql方式
MySQLi extension ("i" 意为 improved)
PDO (PHP Data Objects)
PDO 应用在 12 种不同数据库中, MySQLi 只针对 MySQL 数据库。
phpinfo() 查看是否安装PDO
php代码
连接数据库
<?php
//MySQLi - 面向对象
$servername = "localhost";
$username = "root";
$password = "root";
// 创建连接
$conn = new mysqli($servername, $username, $password);
// 检测连接
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
echo "连接成功";
//MySQLi - 面向过程
// 创建连接
$conn = mysqli_connect($servername, $username, $password);
// 检测连接
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "连接成功";
//PDO
try {
$conn = new PDO("mysql:host=$servername;", $username, $password);
echo "连接成功";
} catch (PDOException $e) {
echo $e->getMessage();
}
/*关闭对象*/
//MySQLi - 面向对象
$conn->close();
//MySQLi - 面向过程
mysqli_close($conn);
//PDO
$conn = null;
标签:数据库,echo,PDO,MySQLi,PHP,连接,conn
From: https://www.cnblogs.com/aeolian/p/18028084