php实现数据库增删改查
作者:野牛程序员:2023-11-14 11:28:13php阅读 2487
连接数据库:
$servername = "数据库服务器地址"; $username = "数据库用户名"; $password = "数据库密码"; $dbname = "数据库名"; $conn = new mysqli($servername, $username, $password, $dbname); if ($conn->connect_error) { die("连接失败: " . $conn->connect_error); }
插入数据:
$sql = "INSERT INTO 表名 (列1, 列2, 列3) VALUES ('值1', '值2', '值3')"; if ($conn->query($sql) === TRUE) { echo "插入成功"; } else { echo "Error: " . $sql . "<br>" . $conn->error; }
查询数据:
$sql = "SELECT 列1, 列2, 列3 FROM 表名"; $result = $conn->query($sql); if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { echo "列1: " . $row["列1"]. " - 列2: " . $row["列2"]. " - 列3: " . $row["列3"]. "<br>"; } } else { echo "0 结果"; }
更新数据:
$sql = "UPDATE 表名 SET 列1='新值1', 列2='新值2' WHERE 条件"; if ($conn->query($sql) === TRUE) { echo "更新成功"; } else { echo "Error updating record: " . $conn->error; }
删除数据:
$sql = "DELETE FROM 表名 WHERE 条件"; if ($conn->query($sql) === TRUE) { echo "删除成功"; } else { echo "Error deleting record: " . $conn->error; }
关闭数据库连接:
$conn->close();
野牛程序员教少儿编程与信息学奥赛-微信|电话:15892516892
- 上一篇:php怎么从表单修改数据库
- 下一篇:PHP删除数据库的某一条数据