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: 1252
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

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

Postby cecilwhitmore » June 25th, 2018, 4:06 pm

I'm having trouble connecting to SQL server via PHP tools. I've followed the instructions in the post but still cannot connect. Any help would be much appreciated.
Cecil
cecilwhitmore
 
Posts: 1
Joined: June 25th, 2018, 4:03 pm

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

Postby danielpaul » July 23rd, 2018, 11:05 am

Steps to connect LocalDB to Visual Studio Server Explorer

Open command prompt
Run SqlLocalDB.exe start v11.0
Run SqlLocalDB.exe info v11.0
Copy the Instance pipe name that starts with np:\...
In Visual Studio select TOOLS > Connect to Database...
For Server Name enter (localdb)\v11.0. If it didn't work, use the Instance pipe name that you copied earlier. You can also use this to connect with SQL Management Studio.
Select the database on next dropdown list
Click OK

------------------
In order to understand what Sarcomatoid mesothelioma is the rare and also the most aggressive form of cancer. It is usually found in pleural mesothelioma. Diagnosis and treatment.
Vintage Industrial Antique Drafting Tables with Cast Iron Base. Bring enduring style into your office with a Antique Drafting Table or desk.
danielpaul
 
Posts: 1
Joined: July 23rd, 2018, 10:59 am


Return to PHP Tools

Who is online

Users browsing this forum: No registered users and 33 guests

cron

User Control Panel

Login

Who is online

In total there are 33 users online :: 0 registered, 0 hidden and 33 guests (based on users active over the past 5 minutes)
Most users ever online was 256 on March 28th, 2024, 9:42 am

Users browsing this forum: No registered users and 33 guests