What are the best practices for optimizing PHP code to efficiently output specific data from a MySQL database without using arrays?
To optimize PHP code for efficiently outputting specific data from a MySQL database without using arrays, one approach is to fetch data row by row using a while loop and directly output the desired data. This method reduces memory usage and improves performance compared to storing all data in an array before outputting.
// Connect to MySQL database
$connection = mysqli_connect("localhost", "username", "password", "database");
// Query to fetch specific data
$query = "SELECT column1, column2 FROM table";
$result = mysqli_query($connection, $query);
// Output data row by row
while ($row = mysqli_fetch_assoc($result)) {
echo "Column 1: " . $row['column1'] . "<br>";
echo "Column 2: " . $row['column2'] . "<br>";
}
// Close database connection
mysqli_close($connection);