简体   繁体   English

具有Laravel连接到SQL Server的Azure Web应用

[英]Azure web app with Laravel connection to SQL server

I have an Azure free trial account. 我有一个Azure免费试用帐户。 This is what I have in the Azure portal (everything in this list is, of course, an Azure resouce): 这就是我在Azure门户中拥有的(当然,此列表中的所有内容都是Azure资源):

  • PHP web app with a working Laravel framework installed 安装了可正常运行的Laravel框架的 PHP Web应用
  • The web app is connected (and update itself) to a Bitbucket repository 该Web应用已连接(并自行更新)到Bitbucket存储库
  • A SQL database (and the related SQL server) SQL数据库(和相关的SQL Server)

Now the question is, how can I do the connection of the Laravel web app with the SQL database? 现在的问题是, 如何连接Laravel Web应用程序和SQL数据库?

Other important details: 其他重要细节:

  1. In the solution, I would like to stick we the free plan (so, please, no solution involving a changing in the Azure plan); 在解决方案中,我想坚持使用免费计划(因此,请不要使用涉及更改Azure计划的解决方案);
  2. I tried to do the connection locally (from a sample code that I got online) but I was not able because of this error: 我试图在本地进行连接(通过在线示例代码进行连接),但由于此错误而无法执行以下操作:

PHP Fatal error: Uncaught Error: Call to undefined function sqlsrv_connect()

I discovered that this error was because I don't have this driver installed locally. 我发现此错误是因为我没有在本地安装驱动程序。 I didn't even try to install it because my "real use" will be from the Laravel website on Azure. 我什至没有尝试安装它,因为我的“实际使用”将来自Azure上的Laravel网站。


<?php
    $serverName = "****.database.windows.net";
    $connectionOptions = array(
        "Database" => "****",
        "Uid" => "admin-****",
        "PWD" => "#####"
    );

    //Establishes the connection
    $conn = sqlsrv_connect($serverName, $connectionOptions);
    $tsql= "SELECT TOP 20 pc.Name as CategoryName, p.name as ProductName
            FROM [SalesLT].[ProductCategory] pc
            JOIN [SalesLT].[Product] p
        ON pc.productcategoryid = p.productcategoryid";
    $getResults= sqlsrv_query($conn, $tsql);

    echo ("Reading data from table" . PHP_EOL);
    if ($getResults == FALSE)
        echo (sqlsrv_errors());
    while ($row = sqlsrv_fetch_array($getResults, SQLSRV_FETCH_ASSOC)) {
        echo ($row['CategoryName'] . " " . $row['ProductName'] . PHP_EOL);
    }
    sqlsrv_free_stmt($getResults);
?>

I dont know why you are using this code in laravel. 我不知道您为什么在laravel中使用此代码。

For Sql Server Connection laravel provides drivers, use that for your case it will be: 对于Sql Server Connection,laravel提供了驱动程序,在您的情况下使用它将是:

In config folder > databse file edit these : 在配置文件夹>数据库文件中编辑以下内容:

  1. 'default' => env('DB_CONNECTION', 'mysql'), to 'default' => env('DB_CONNECTION', 'mysql'),

    'default' => 'sqlsrv',

    if you dont want to use env file vars else update env var to reflect the same. 如果您不想使用env文件vars,请更新env var以反映相同的内容。

  2. update this driver values: 'connections' => [ 'sqlsrv' => [, 更新此驱动程序值: 'connections' => [ 'sqlsrv' => [,

And for more help on deploying laravel app on azure check this: https://medium.com/@coderonfleek/hosting-a-laravel-application-on-azure-web-app-b55e12514c46 有关在天蓝色上部署laravel应用的更多帮助,请查看以下网址https ://medium.com/@coderonfleek/hosting-a-laravel-application-on-azure-web-app-b55e12514c46

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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