How can PHP be used to retrieve specific data from a MySQL database for editing purposes?
To retrieve specific data from a MySQL database for editing purposes using PHP, you can use a SELECT query with a WHERE clause to specify the criteria for the data you want to retrieve. You can then fetch the results and display them in a form for editing.
<?php
// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Retrieve specific data for editing
$id = $_GET['id']; // Assuming the id is passed in the URL
$sql = "SELECT * FROM table_name WHERE id = $id";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// Output data of each row
while($row = $result->fetch_assoc()) {
// Display the data in a form for editing
echo "<input type='text' name='name' value='" . $row['name'] . "'>";
echo "<input type='text' name='email' value='" . $row['email'] . "'>";
}
} else {
echo "0 results";
}
$conn->close();
?>