在PHP中,CRUD(Create, Read, Update, Delete)是指对数据库中的数据进行基本操作的四个函数。以下是一个简单的CRUD操作示例:
function create($name, $age, $email) {
$conn = mysqli_connect("localhost", "username", "password", "myDB");
$sql = "INSERT INTO users (name, age, email) VALUES ('$name', '$age', '$email')";
mysqli_query($conn, $sql);
mysqli_close($conn);
}
function read($id) {
$conn = mysqli_connect("localhost", "username", "password", "myDB");
$sql = "SELECT * FROM users WHERE id=$id";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
mysqli_close($conn);
return $row;
}
function update($id, $name, $age, $email) {
$conn = mysqli_connect("localhost", "username", "password", "myDB");
$sql = "UPDATE users SET name='$name', age='$age', email='$email' WHERE id=$id";
mysqli_query($conn, $sql);
mysqli_close($conn);
}
function delete($id) {
$conn = mysqli_connect("localhost", "username", "password", "myDB");
$sql = "DELETE FROM users WHERE id=$id";
mysqli_query($conn, $sql);
mysqli_close($conn);
}
这些函数可以用于处理基本的数据库操作,包括创建、读取、更新和删除数据。在实际应用中,可能需要进行更多的错误处理和安全措施。
领取专属 10元无门槛券
手把手带您无忧上云