What are the differences between using standard SQL queries and prepared statements in PHP when connecting to a Microsoft Azure SQL database?

When connecting to a Microsoft Azure SQL database in PHP, using prepared statements is recommended over standard SQL queries for security reasons. Prepared statements help prevent SQL injection attacks by separating SQL code from user input. This can help protect your database from malicious queries.

// Using prepared statements to connect to a Microsoft Azure SQL database in PHP

$serverName = "your_server.database.windows.net";
$connectionOptions = array(
    "Database" => "your_database",
    "Uid" => "your_username",
    "PWD" => "your_password"
);

$conn = sqlsrv_connect($serverName, $connectionOptions);

if ($conn) {
    $sql = "SELECT * FROM your_table WHERE id = ?";
    $params = array(1);

    $stmt = sqlsrv_query($conn, $sql, $params);

    while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
        echo "Name: " . $row['name'] . "<br />";
    }

    sqlsrv_free_stmt($stmt);
    sqlsrv_close($conn);
} else {
    die(print_r(sqlsrv_errors(), true));
}