How can a reference column be used to control the sorting of data in a PHP MySQL database?
When sorting data in a PHP MySQL database, you can use a reference column to control the sorting order. By assigning a numerical value to each row in the reference column, you can then sort the data based on this column. This allows you to customize the sorting order according to your specific requirements.
// Connect to MySQL database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
$conn = new mysqli($servername, $username, $password, $dbname);
// Select data from table and order by reference column
$sql = "SELECT * FROM table_name ORDER BY reference_column ASC";
$result = $conn->query($sql);
// Output data
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "ID: " . $row["id"]. " - Name: " . $row["name"]. "<br>";
}
} else {
echo "0 results";
}
// Close database connection
$conn->close();