How to connect to local SQL-Server in Visual Studio

PHP Tools [?] extends Visual Studio with set of advanced features to work more efficiently with PHP code.

How to connect to local SQL-Server in Visual Studio

Postby Bluesky787 » July 19th, 2017, 4:14 pm

Hello everyone,

It's really a shame to post this question, but I am a complete PHP newbie and I stumbled upon thousands of different approaches to connect PHP to MSSQL, but not in Visual Studio. Apparently, this is still a bit uncommon...

So, now I have set up a MSSQL-Server from within Visual Studio 2017, where PHP Tools for Visual Studio is installed. The server is hosted locally in a .mdf file. There are already some tables created and VS tells me there's a functioning connection to MSSQL. It also gives me a connection String.

Now I tried to connect using

Code: Select all
$server_db = mysqli_connect('(localdb)\\v13.0', array('Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename="D:\Users\Username\Documents\Visual Studio 2017\Projects\MyProject\SQL_DB.mdf";Integrated Security=True;Connect Timeout=30'));


where the whole string within this connect command is the given connection string I retrieved from VS (in Server-Properties).

So, conclusion is, the program runs this command and returns a null Object.

Most sources I found have stated I should use 'sqlsrv_connect' instead, but this command isn't recognized by VS.

What am I doing wrong?
Help of every kind is highly appreciated.

Regards
Bluesky787
 
Posts: 3
Joined: July 19th, 2017, 3:57 pm

Re: How to connect to local SQL-Server in Visual Studio

Postby Miloslav Beno » July 25th, 2017, 8:39 am

Hello,

This question is not really PHP Tools related, but basicaly issue is that you are trying to connect to mssql server with mysql api.

As you've pointed out you will need to use sqlsrv_* functions, which are not installed by default. You will need to place php_sqlsrv.dll extension in the ext directory of your php installation annd activate the extension in php.ini.

You can get the extension from here https://www.microsoft.com/en-us/download/details.aspx?id=20098

I hope that will help.

Thanks,
Miloslav Beno │ DEVSENSE s.r.o. │ @miloslavbenomiloslav@devsense.com
User avatar
Miloslav Beno
 
Posts: 1073
Joined: January 7th, 2012, 8:36 pm

Re: How to connect to local SQL-Server in Visual Studio

Postby Bluesky787 » August 12th, 2017, 10:04 am

Hello Miloslav,

I could verify your answer yesterday and it worked for me.
Thanks.

Best Regards.
Bluesky787
 
Posts: 3
Joined: July 19th, 2017, 3:57 pm

Re: How to connect to local SQL-Server in Visual Studio

Postby Bluesky787 » August 12th, 2017, 10:49 am

If someone else has the same problem, here are some more detailed info:
The link specified by Miloslav will give you the choice of 4 different driver downloads (SQLSERVER 3.0, 3.1, 3.2 and 4.0). If you work with >=PHP 7.0, 4.0 is yours (at the time of this writing).

Extract the files to the ext folder in your PHP installation directory. You could see it by printing phpinfo() in your php project. When in doubt, standard folder in PHPTools seems to be
%SystemDrive%\Program Files (x86)\IIS Express\PHP\v7.0\ext (on x64 systems)
%SystemDrive%\Program Files\IIS Express\PHP\v7.0\ext (on x86 systems)

In php.ini File, you have to specify the ext-directory and the addons you installed and want to use. Choose the extension you need or, when in doubt, all. For me, it works with the first four extensions enabled:

Following lines must be added (without ''):
'extension_dir = "ext"'
'extension=php_sqlsrv_7_nts_x64.dll' (or)
'extension=php_sqlsrv_7_nts_x86.dll' (or)
'extension=php_sqlsrv_7_ts_x64.dll' (or)
'extension=php_sqlsrv_7_ts_x86.dll' (or)

'extension=php_pdo_sqlsrv_7_nts_x64.dll' (or)
'extension=php_pdo_sqlsrv_7_nts_x86.dll' (or)
'extension=php_pdo_sqlsrv_7_ts_x64.dll' (or)
'extension=php_pdo_sqlsrv_7_ts_x86.dll'

Now everything should be set up correctly.
Regards and happy coding :)

Edit: I am also required to download and install the Microsoft ODBC driver 11 for SQL Server, which is available here:
https://www.microsoft.com/en-us/download/confirmation.aspx?id=36434
Bluesky787
 
Posts: 3
Joined: July 19th, 2017, 3:57 pm


Return to PHP Tools

Who is online

Users browsing this forum: No registered users and 8 guests

cron

User Control Panel

Login

Who is online

In total there are 8 users online :: 0 registered, 0 hidden and 8 guests (based on users active over the past 5 minutes)
Most users ever online was 62 on January 7th, 2012, 10:54 pm

Users browsing this forum: No registered users and 8 guests
Phalanger