// 连接MySQL数据库 $conn = mysqli_connect($servername, $username, $password, $dbname); if (!$conn) { die("连接失败: " . mysqli_connect_error()); } // 创建用户表 $sql = "CREATE TABLE users ( id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, username VARCHAR(30) NOT NULL, password VARCHAR(30) NOT NULL )"; if (mysqli_query($conn, $sql)) { echo "用户表创建成功"; } else { echo "Error creating table: " . mysqli_error($conn); } // 插入用户 $sql = "INSERT INTO users (username, password) VALUES ('John', 'Doe')"; if (mysqli_query($conn, $sql)) { echo "用户插入成功"; } else { echo "Error: " . $sql . "
" . mysqli_error($conn); } // 查询用户 $sql = "SELECT * FROM users"; $result = mysqli_query($conn, $sql); if (mysqli_num_rows($result) >0) { while($row = mysqli_fetch_assoc($result)) { echo "id: " . $row["id"]. " - Name: " . $row["username"]. " " . $row["password"]. "
"; } } else { echo "0 结果"; } // 更新用户信息 $sql = "UPDATE users SET username='Jane' WHERE id=1"; if (mysqli_query($conn, $sql)) { echo "用户信息更新成功"; } else { echo "Error updating record: " . mysqli_error($conn); } // 删除用户 $sql = "DELETE FROM users WHERE id=1"; if (mysqli_query($conn, $sql)) { echo "用户删除成功"; } else { echo "Error deleting record: " . mysqli_error($conn); } // 关闭数据库连接 mysqli_close($conn);
通过以上代码示例,大家可以看出,开发MySQL开发论坛需要先创建一个用户表,再进行用户相关的操作,如插入用户数据、查询用户数据、更新用户数据、删除用户数据等。 此外,为了确保数据的安全性,大家还需要在代码中做好数据库连接关闭的操作。