How can MySQL be integrated with PHP to facilitate sorting and ordering of links?
To facilitate sorting and ordering of links using MySQL and PHP, you can query the database to retrieve the links in the desired order. By using the ORDER BY clause in your SQL query, you can sort the links based on a specific column such as title or date. Then, you can use PHP to fetch and display the sorted links on your webpage.
// Connect to MySQL database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "links_db";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Query to retrieve links ordered by title
$sql = "SELECT * FROM links_table ORDER BY title";
$result = $conn->query($sql);
// Display sorted links
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<a href='" . $row["url"] . "'>" . $row["title"] . "</a><br>";
}
} else {
echo "No links found.";
}
$conn->close();