我在Azure上有一个PHP应用程序,我希望此应用程序连接到Azure SQL数据库。
$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', 'john@example.com')";
mssql_query($sql, $conn);
mssql_close($conn);
当我运行这个PHP脚本时,我收到500错误。
您是否具有使用Azure和Azure SQL数据库的PHP的经验?或者必须在Azure上为Azure SQL数据库设置一些东西?
答案 0 :(得分:2)
默认情况下,Azure环境不会安装php_mssql.dll扩展名,而是安装php_sqlsrv.dll。
您可以使用sqlsrv()代替:
$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));
}