Categories
Database Linux Mac PHP Programming

PHP: Connect to SQL Server Database on Linux (and Mac OS X)

Microsoft SQL Server
Microsoft SQL Server

I have used PHP to connect to SQL Server plenty of time, and every time I would use the SQLSRV extension. The problem with that is, PHP’s SQLSRV extension is only available for Windows servers. Which wasn’t a problem for me because I would normally develop PHP on Windows machines. It wasn’t until recently I needed SQL Server access on a Linux machine.

It wasn’t as straightforward as I had hoped, and it certainly did not help to learn that the MSSQL PHP extension was removed in version 7.0, which took me a few hours to discover. I am writing this post in hopes that I save some people a few hours of pain, including my future self.

Advertisement
Categories
Database

Installing Microsoft SQL Server Data Tools – Business Intelligence for Visual Studio 2013: Rule “Same architecture installation” failed??

SSDTFailed
So I had to install Microsoft SQL Server Data Tools – Business Intelligence for Visual Studio 2013 today, and was greeted with the above error. Based on the error, I assumed I had SQL Server 2014 x64 installed and I somehow needed SSDT for x64 and not x86. Funnily enough, the installation page suggests both x64 and x86 architectures are supported. I was frantically searching for a x64 installation.

Thanks to a StackOverflow post:

As the others have said, there is only a 32 bit version.

The important thing is, if you’re running an x64-based SQL instance (64-bit), make sure to select “New Instance” on the Installation Type page, and NOT “Add features to an existing instance”. Not doing so will cause it to error out.

I was selecting “Add features to an existing instance” thinking “New Instance” meant installing a new SQL Server fresh again, like most people would.

Categories
Database

SQL Server: Taking Advantage of Transactions

I won’t go in depth about SQL Server transactions, but rather what you can do to apply destructive changes safely, like deleting records or updating them. It’s highly advised that direct queries don’t get run on production databases but there are situations where that is required.

Categories
Database Programming

SQL Server: Selecting multiple rows into a single row with multiple columns

I ran into a problem writing a query today, and took the time to find the solution, so I thought I should share this with anyone who has the same problem. This solution should only work for SQL Server version 2005 and above.

Categories
Database Programming

SQL Server: SELECT INTO inside both IF and ELSE blocks

I ran across this problem earlier today, and thought I should address this here in case anyone is interested in my solution.

When doing a SELECT INTO command against the same temp table in both IF and ELSE blocks, you will quickly learn that SQL Server complains that there “is already an object named ‘#temp’ in the database”. What one can do to avoid this problem is declare the temp table before the IF/ELSE block and insert into the table afterwards, but what if you want to use SELECT INTO instead?

if @var = 1
	select 'abc' into #temp;
else
	select 'def' into #temp;

select * from #temp;