Gangrel
Gangrel

Reputation: 481

Writing to Azure SQL Database from PHP application

I would like to know if it's possible to write directly to an azure sql database from a PHP form within an application.

So for example I could have a 3 field form with something like "ID", "Name", "Email" and then on submit that would go straight into the database.

I know this is easily done with mySQL but I have a specific requirement for using Azure.

Thanks

Upvotes: 0

Views: 73

Answers (1)

zak
zak

Reputation: 71

Yes, you can.

If you are using linux, you should :

Install the Microsoft ODBC driver for SQL Server

Install the PHP drivers for Microsoft SQL Server

Then it's pretty straightforward :

<?php
    $serverName = "your_server.database.windows.net"; // update me
    $connectionOptions = array(
        "Database" => "your_database", // update me
        "Uid" => "your_username", // update me
        "PWD" => "your_password" // update me
    );
    //Establishes the connection
    $conn = sqlsrv_connect($serverName, $connectionOptions);
    $tsql= "SELECT TOP 20 pc.Name as CategoryName, p.name as ProductName
         FROM [SalesLT].[ProductCategory] pc
         JOIN [SalesLT].[Product] p
         ON pc.productcategoryid = p.productcategoryid";
    $getResults= sqlsrv_query($conn, $tsql);
    echo ("Reading data from table" . PHP_EOL);
    if ($getResults == FALSE)
        echo (sqlsrv_errors());
    while ($row = sqlsrv_fetch_array($getResults, SQLSRV_FETCH_ASSOC)) {
     echo ($row['CategoryName'] . " " . $row['ProductName'] . PHP_EOL);
    }
    sqlsrv_free_stmt($getResults);
?>

Full Documentation for MS

Full Documentation for PHP SQLSRV functions

Upvotes: 1

Related Questions