How can specific data be retrieved from a MySQL table based on an ID or other information in PHP?
To retrieve specific data from a MySQL table based on an ID or other information in PHP, you can use a SQL query with a WHERE clause to filter the results. The WHERE clause allows you to specify a condition that must be met for the data to be included in the result set. By passing the ID or other information as a parameter in the query, you can retrieve the specific data you need.
<?php
// Connect to MySQL 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 data based on ID
$id = 1; // ID of the data to retrieve
$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()) {
echo "ID: " . $row["id"]. " - Name: " . $row["name"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
Keywords
Related Questions
- How can you convert a date stored in an array from a MySQL query result into a single string for further processing in PHP?
- Are there any security considerations to keep in mind when using PHP to handle form data and redirect to external URLs?
- What are the limitations of using PHP for real-time user tracking or monitoring?