为了在 Azure 上运行 PHP webjob 以将记录插入 SQL 数据库,我的代码不断中断$query->execute();
。
这就是我所做的。首先,我进入我的 SQL 数据库并在master
db 下创建了一个登录名:
CREATE LOGIN username WITH password='userpassword'
然后,我将此登录名添加为当前数据库中的用户(不是在 下master
,而是在 下mydb
):
CREATE USER username FROM LOGIN username
然后我用这个命令给了这个用户写权限:
EXEC sp_addrolemember N'db_datawriter', N'username'
完成所有这些后,我进入了 portal.azure,打开了我的应用服务,导航到 WebJobs 并上传了一个.zip
包含两个文件的文件;
调度程序,settings.job
:
{
"schedule": "0 */5 * * * *"
}
还有我的主要 PHP 代码updateRecord.php
:
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
try {
$conn = new PDO ( "sqlsrv:server = myazuresite.database.windows.net,1433; Database = mydb", "username", "userpassword");
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
}
catch ( PDOException $e ) {
print( "Error connecting to SQL Server." );
}
$cm = 'cm';
$span = '1day';
$stf = $conn->prepare("INSERT INTO mydbtable
VALUES ( $cm, $span, 1, 2, 3, 4, 12.34 );");
$stf->execute();
echo $stf;
unset($conn);
unset($stmt);
?>
当我启动 WebJob 时,它从Running
状态变为Pending restart
.
[09/10/2016 21:03:14 > 82e662: SYS INFO] Detected WebJob file/s were updated, refreshing WebJob
[09/10/2016 21:03:14 > 82e662: SYS INFO] Status changed to Starting
[09/10/2016 21:03:14 > 82e662: SYS INFO] Run script 'updateRecord.php' with script host - 'PhpScriptHost'
[09/10/2016 21:03:14 > 82e662: SYS INFO] Status changed to Running
[09/10/2016 21:03:15 > 82e662: INFO]
[09/10/2016 21:03:15 > 82e662: INFO] Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: [Microsoft][SQL Server Native Client 11.0][SQL Server]Incorrect syntax near 'day'.' in D:\local\Temp\jobs\continuous\myjobname\uf5ls33g.42d\myjobzipfolder\updateRecord.php:20
[09/10/2016 21:03:15 > 82e662: INFO] Stack trace:
[09/10/2016 21:03:15 > 82e662: INFO] #0 D:\local\Temp\jobs\continuous\gAnaytics\uf5ls33g.42d\myjobzipfolder\updateRecord.php(20): PDOStatement->execute()
[09/10/2016 21:03:15 > 82e662: INFO] #1 {main}
[09/10/2016 21:03:15 > 82e662: INFO] thrown in D:\local\Temp\jobs\continuous\jobs\uf5ls33g.42d\myjobzipfolder\updateRecord.php on line 20
[09/10/2016 21:03:15 > 82e662: SYS ERR ] Job failed due to exit code 255
[09/10/2016 21:03:15 > 82e662: SYS INFO] Process went down, waiting for 60 seconds
[09/10/2016 21:03:15 > 82e662: SYS INFO] Status changed to PendingRestart
整个 WebJob 日志中最相关且最相关的是:
[09/10/2016 21:03:15 > 82e662: INFO] 致命错误:未捕获的异常 'PDOException' 带有消息'SQLSTATE[42000]:[Microsoft][SQL Server Native Client 11.0][SQL Server]'附近的语法不正确天'。' 在 D:\local\Temp\jobs\continuous\myjobname\uf5ls33g.42d\myjobzipfolder\updateRecord.php:20
谁能诊断如何解决这个问题?我没有看到我如何准备我的 sql 语句或执行它有什么问题。