3

I have a PHP application on Azure and I want this application to connect to Azure SQL Database.

$servername = "AZURE.database.windows.net";
$username = "user";
$password = "pass";
$dbname = "DB";

$conn= mssql_connect($servername,$username,$password, $dbname);

$sql = "INSERT INTO AOL (AgentId,FullName, Email)
VALUES ('1', 'Doe', '[email protected]')";


mssql_query($sql, $conn);
mssql_close($conn);

When I run this php script I get a 500 error.

Do you have experience with PHP on Azure and Azure SQL Database? Or must setting something on Azure for Azure SQL Database?

4
  • do you use Sql Azure? if so, make sure you have config your firewall rules to allow your site to connect to it. Commented Jan 12, 2016 at 22:22
  • yes, i using azure sql and firewall has rules for my websites :/ Commented Jan 12, 2016 at 22:35
  • What format are you using for username? Also, for clarification, since you're connecting to SQL Database service (and not MS SQL Server), I changed the tagging accordingly. Commented Jan 12, 2016 at 22:39
  • What does your table definition (especially column types) look like? Commented Jan 12, 2016 at 23:06

1 Answer 1

3

By default, Azure environment doesn't install php_mssql.dll extension, it installs php_sqlsrv.dll instead.

You can use sqlsrv() instead:

$serverName = "<servername>.database.windows.net, 1433"; //serverName\instanceName
$connectionInfo = array("Database" => "<database_name>", "UID" => "<userid>", "PWD" => "<passowrd>");
$conn = sqlsrv_connect($serverName, $connectionInfo);

if ($conn) {
    echo "Connection established.<br />";

    $query = sprintf("SELECT 1 as test");
    $stmt = sqlsrv_query($conn, $query);
    if ($stmt === false) {
        die(print_r(sqlsrv_errors(), true));
    }

    while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
        print_r($row);
    }

    sqlsrv_free_stmt($stmt);

} else {
    echo "Connection could not be established.<br />";
    die(print_r(sqlsrv_errors(), true));
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.