简体   繁体   中英

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 .zip file containing a .php and a .json 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 .zip into webjobs, the Status is always "Pending Restart".

Here's what I have in my .php file:

<?php

$conn = new PDO ( "sqlsrv:server = mydb.database.windows.net,1433; Database = myappservices");
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
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);");
  $stf->execute();
}

?>

Then my .json file is just a scheduler:

{
    "schedule": "0 */5 * * * *"
}

These are the only two files in my .zip 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.

Consider this job.php :

<?php

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

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");
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e) {
    print("Error connecting to SQL Server: " + $e);
}

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

unset($conn);
unset($stmt);

?>    

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

Array
(
    [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
)
Array
(
    ...
)
...

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:

流程浏览器

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:
https://azure.microsoft.com/en-us/documentation/articles/sql-database-aad-authentication/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM