VolcovMeter VolcovMeter - 1 year ago 104
SQL Question

Azure: How to correctly use a PHP webjob to make changes to SQL database?

GOAL: To use a PHP webjob to connect and alter SQL database table in Azure.

I'm trying to upload a

file containing a
and a
file to the webjobs settings inside of an app service I have running on Azure.

I believe there's something wrong with the way I'm coding the PDO-SQL connection inside of the PHP file, when I upload the webjob as a
into webjobs, the Status is always "Pending Restart".

Here's what I have in my


$conn = new PDO ( "sqlsrv:server = mydb.database.windows.net,1433; Database = myappservices");
catch ( PDOException $e ) {
print( "Error connecting to SQL Server." );
$connectionInfo = array("Database" => "myappservices");
$serverName = "mydb.database.windows.net,1433";
$conn = sqlsrv_connect($serverName, $connectionInfo);

if ($conn) {
$stf = $conn->prepare("INSERT INTO MyTable
VALUES ('boom', 1, 2);");


Then my
file is just a scheduler:

"schedule": "0 */5 * * * *"

These are the only two files in my
file I'm uploading.

To explain the PHP code, I'm trying to connect via windows authentication (no need for user/pass). Maybe I'm doing this wrong too.

Anyone have any ways to do this? I would really appreciate giving a step by step or suggestions as to how to change my code to get this webjob to actually run.

Answer Source

Consider this job.php:


ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);

try {
    // DON'T HARDCODE CREDENTIALS, pull from Application Settings.
    // In Azure App Service, Application Settings are exposed as
    // environment variables.
    // i.e. $db_user = getenv("DB_USER");
    $conn = new PDO ("sqlsrv:server = poqfsXXXX.database.windows.net,1433;
                     Database = MobileApp_db",
                     "Username", "P@ssw0rd");
catch (PDOException $e) {
    print("Error connecting to SQL Server: " + $e);

$stmt = $conn->prepare("select * from todoitems");
while ($row = $stmt->fetch()) {



Expected output:
(You can test in Kudu: https://{sitename}.scm.azurewebsites.net/DebugConsole)

d:\home\site\tests> "d:\program files (x86)\php\v5.6\php.exe" job.php

    [Id] => d4657cff-09a2-4a8a-b1d2-462c2c42a9f0
    [0] => d4657cff-09a2-4a8a-b1d2-462c2c42a9f0
    [Text] => From Azure SQL
    [1] => From Azure SQL
    [Complete] => 0
    [2] => 0
    [Version] => 0000000000001825
    [3] => 0000000000001825
    [CreatedAt] => 2016-06-17 10:11:17.1167267 +00:00
    [4] => 2016-06-17 10:11:17.1167267 +00:00
    [UpdatedAt] => 2016-06-17 10:11:17.1167267 +00:00
    [5] => 2016-06-17 10:11:17.1167267 +00:00
    [Deleted] => 0
    [6] => 0

To explain the PHP code, I'm trying to connect via windows authentication (no need for user/pass). Maybe I'm doing this wrong too.

While Azure SQL does support Windows Authentication, i'm not exactly sure what you mean by "no need for user/pass". Windows Authentication means "take the credentials of whomever this process is running as and attempt authentication against the SQL server". Since you're running your Webjob as a random user provided by the sandbox, Windows Authentication doesn't make much sense.

From Kudu's Process Explorer:

Process Explorer

Here's one valid scenario for Windows Authentication with Azure SQL:

You have an on-prem hosted application that uses Active Directory to authenticate to your on-prem SQL Server. You have a requirement to move to Azure SQL. You do not have the option to change the authentication method for SQL. So you dirsync your directory to Azure AD and use Windows Authentication to connect to Azure SQL.

More on Windows Authentication in Azure SQL: