Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preferred method of accessing MS SQL with PHP 5.3+ on Linux

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!

like image 966
Greg Lamb Avatar asked May 18 '11 17:05

Greg Lamb


People also ask

How connect MS SQL with PHP?

Running MS SQL Queries from PHP See the full list of MS SQL functions in PHP at php.net. The following script is an example of an MS SQL SELECT query run from PHP. <? php $query ="SELECT col1,col2 FROM tablename"; $result =mssql_query($query); while ( $record = mssql_fetch_array($result) ) { echo $record["col1"] .

How do I connect to SQL Server database from Linux?

To connect to a SQL Server Express instance, use the format machinename \SQLEXPRESS . If the SQL Server instance is listening on the default port, leave this set to 1433 . If your database administrator told you to specify a different port, replace 1433 with the new port number. Otherwise, delete 1433 .

Can we use mssql with PHP?

Yes, you can. It depends on which version of PHP you're using, but if you're using PHP5+ you can use Microsoft's SQL Server Driver for PHP. Make sure you use version 2, which gives you the PDO functionality as well as the procedural style.


1 Answers

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);
like image 154
Brian Showalter Avatar answered Oct 20 '22 20:10

Brian Showalter