How can SQL statements be used as an alternative to array manipulation in PHP for data processing tasks?

When dealing with data processing tasks in PHP, using SQL statements can be a more efficient alternative to array manipulation. By querying a database using SQL, you can easily filter, sort, and manipulate data without the need for complex array functions. This can lead to faster and more streamlined data processing operations.

// Connect to database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database_name";

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// SQL query to retrieve data
$sql = "SELECT * FROM table_name WHERE condition = 'value'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // Output data of each row
    while($row = $result->fetch_assoc()) {
        echo "Column1: " . $row["column1"]. " - Column2: " . $row["column2"]. "<br>";
    }
} else {
    echo "0 results";
}

$conn->close();