如何用php新增mysql問題
时间: 2024-04-29 14:25:01 浏览: 38
PHP获取MySql新增记录ID值的3种方法
可以使用以下代码实现新增 MySQL 问题:
```
<?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 查询
$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john@example.com')";
if ($conn->query($sql) === TRUE) {
echo "新纪录插入成功";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
// 关闭数据库连接
$conn->close();
?>
```
阅读全文