PHP 连接 MySQL
PHP 5 及以上版本建议使用以下方式连接 MySQL :
MySQLi extension (“i” 意为 improved)
PDO (PHP Data Objects)
在 PHP 早起版本中我们使用 MySQL 扩展。但该扩展在 2012 年开始不建议使用。
这里我使用MySQLi extension。
MySQLi extension有两种方式,面向对象和面向过程。注意下面面向对象好面向过程的区别:
面向对象:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);//使用new mysqli()来创建连接。
// 检测连接
if ($conn->connect_error) {//对象$conn有一个connect_error属性。
die("连接失败: " . $conn->connect_error);
}
// 使用 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
)";
if ($conn->query($sql) === TRUE) {//$对象中的query()方法可以把sql语句发送给数据库。
echo "Table MyGuests created successfully";
} else {
echo "创建数据表错误: " . $conn->error;//输出错误原因
}
$conn->close();//对象中有一个close()方法可以关闭数据库。
?>
面向过程:
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "myDB";
// 创建连接
$conn = mysqli_connect($servername, $username, $password, $dbname);//直接使用 mysqli_connect()来创建连接。
// 检测连接
if (!$conn) {
die("连接失败: " . mysqli_connect_error());
}
// 使用 sql 创建数据表
$sql = "CREATE TABLE MyGuestTTs (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP
)";
if (mysqli_query($conn,$sql) === TRUE) {//mysql_query()方法可以把sql语句发送给数据库。
echo "Table MyGuests created successfully";
} else {
echo "创建数据表错误: " . mysqli_error($conn);//输出错误原因
}
mysqli_close($conn);//关闭数据库。
?>
插入数据
面向对象:
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "myDB";
//创建连接
$conn = new mysqli($servername,$username,$password,$dbname);
//检测连接
if ($conn->connect_error) {
die("连接错误".$conn->connect_error);
};
$sql = "INSERT INTO MyGuests(firstname,lastname,email)
VALUE ('LIU','XUAN','XUAN@QQ.COM')";
if ($conn->query($sql) === TRUE) {
echo "新纪录插入成功";
} else {
echo "新剧路插入失败".$conn->error;
}
$conn->close();
?>
面向过程:
php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "myDB";
//创建连接
$conn = mysqli_connect($servername,$username,$password,$dbname);
//检测连接
if (!$conn) {
die("连接错误".mysqli_connect_error());
};
$sql = "INSERT INTO MyGuests(firstname,lastname,email)
VALUE ('LIU','XUAN','XUAN@QQ.COM')";
if (mysqli_query($conn,$sql) === true) {
echo "新纪录插入成功";
} else {
echo "新剧路插入失败".mysqli_error($conn);
}
mysqli_close($conn);
?>
同时插入多条数据
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// 创建链接
$conn = new mysqli($servername, $username, $password, $dbname);
// 检查链接
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john@example.com');";
$sql .= "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('Mary', 'Moe', 'mary@example.com');";
$sql .= "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('Julie', 'Dooley', 'julie@example.com')";
if ($conn->multi_query($sql) === TRUE) {//multi_query()用于同时插入多条数据。面向过程时这样写mysqli_multi_query($conn,$sql)
echo "新记录插入成功";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>