9

What is the preferred method of accessing a Microsoft SQL Server database with PHP 5.3+ on Linux?

Given the different extension options now available I'm unsure which method is preferred based on reliability and performance. Right now I am using the mssql extension with FreeTDS, but I'd like to know if this isn't the best way.

I've heard some recommend using php-odbc/EasySoft because the mssql extension has been abandoned - yet others have said going the ODBC route isn't worth the performance hit.

The response to this stackoverflow question seems to touch on what I'm asking, however it's Windows centric.

Thank you!

1

2 Answers 2

1

I've had good results using PHP's PDO (PHP Data Objects) library for this sort of thing. There's an excellent tutorial at http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/. Just make sure you use the following template to initialize your database connection:

$dbh = new PDO("dblib:host=$host;dbname=$dbname", $user, $pass);
Sign up to request clarification or add additional context in comments.

2 Comments

I agree PDO can be a better way to go when possible, but in this case I'm wondering which driver is best? PDO_ODBC vs PDO_DBLIB vs Non-PDO mssql? And is it apples and oranges to compare these on Windows vs Linux?
Well, I like PDO because it's simple and abstracts the details of a database connection so you can use the same code with different back-end databases by simply changing the driver name. I would generally favor a direct, native connection to any database where possible over an ODBC connection, because ODBC does have more overhead and is more complex to install and configure. As for comparing Windows and Linux, I have not used PHP on Windows so I'm afraid I can't help there.
0

I use FreeTDS. It's far from satisfactory. In fact, I found your question because I live in hope that there must be a better solution and I keep looking for one. Unfortunately, FreeTDS seems to be the best option at the moment, if you're developing in a Linux environment.

If you were on Windows, you could obviously use the Microsoft SQL Server drivers for PHP which would be better, but even then there are missing features, like support for PDO lastInsertId().

Comments

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.