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!
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"] .
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 .
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With