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

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 PHP Programming

PHP: sqlsrv problems with UTF-8 values? Set your CharacterSet option!

Are you banging your head trying to figure out how to convert a seemingly invalid UTF-8 value into a valid one? You might be surprised to know that the problem isn’t how PHP interprets the characters, but rather a database connection issue. If the UTF-8 value in question looks valid in the database but does not look right when displayed on a web page, that could mean your database connection settings are not set correctly.

Categories
Database PHP Programming

PHP: Importing an SQL file via PHP script

Trying to import an SQL file using PHP? Most people will tell you that using PHP function shell_exec to run the MySQL client is your best bet, but what if you simply can’t do it that way? The solution is quite simple, actually, and doesn’t require programming skills beyond the basic file parsing and query executing.

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
js PHP Programming

Data scraping: how to prevent

Data scraping is common these days with so many data driven web applications out there. Regardless of the legality and ethics of the subject, it just sucks to know that there may be people out there who might hammer at your site to “take” all your hard work for their selfish use. That being said, it’s almost impossible to prevent people from taking your data, especially if your data is easily accessible by the public.

You’re probably here because you’re suspicious of possible data scraping activities happening in your website. While there are preventive measures you can take to keep the scrapers out, there are always holes for the smart ones to get through and eventually take the data they want. What we want to do is make it very difficult for the average scraper to hack away the data.

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;