1

I have an Azure free trial account. This is what I have in the Azure portal (everything in this list is, of course, an Azure resouce):

  • PHP web app with a working Laravel framework installed
  • The web app is connected (and update itself) to a Bitbucket repository
  • A SQL database (and the related SQL server)

Now the question is, how can I do the connection of the Laravel web app with the SQL database?

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);
  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.


<?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);
?>
2
  • Please share a little of your laravel .env file it might help in understanding better what connections your are making to connect the services. Commented Oct 4, 2018 at 6:40
  • @somsgod I didn't put anything in my .env file. The test I runned locally (where I got the error) was with a simple php code I found online (no Laravel). I edited the post and added the code. Commented Oct 4, 2018 at 6:45

1 Answer 1

4

I dont know why you are using this code in laravel.

For Sql Server Connection laravel provides drivers, use that for your case it will be:

In config folder > databse file edit these :

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

    'default' => 'sqlsrv',

    if you dont want to use env file vars else update env var to reflect the same.

  2. update this driver values: '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

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much @somsgod! It worked perfectly. I just added a new 'connection' in database.php named Azure and set it as the default one. PS: the code I posted above was just a test that I run locally to make sure everything was fine with the database I created. PPS: I already knew (and followed) the guide you linked me for creating the Azure web app, a big shoutout to Fikayo Adepoju for creating an amazing guide. Cheers! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.