Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect to mssql using pdo through PHP and Linux?

I'm trying to for a new PDO connection using the following code.

new PDO("mssql:driver=????;Server={$serverName};Database={$databaseName}", $username, $password, array(PDO::ATTR_PERSISTENT => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)); 

I'm not sure what drivers to use? Or how to install them. I can connect perfectly fine using the mssql_connect function in PHP but I want to use the PDO library instead.

My php.ini settings for mssql are:

ssql  MSSQL Support   enabled Active Persistent Links     0 Active Links    1 Library version     FreeTDS  Directive   Local Value Master Value mssql.allow_persistent  On  On mssql.batchsize 0   0 mssql.charset   no value    no value mssql.compatability_mode    Off Off mssql.connect_timeout   5   5 mssql.datetimeconvert   On  On mssql.max_links Unlimited   Unlimited mssql.max_persistent    Unlimited   Unlimited mssql.max_procs Unlimited   Unlimited mssql.min_error_severity    10  10 mssql.min_message_severity  10  10 mssql.secure_connection Off Off mssql.textlimit Server default  Server default mssql.textsize  Server default  Server default mssql.timeout   60  60 
like image 543
richie Avatar asked May 10 '11 17:05

richie


People also ask

How do I connect to a PDO database?

A PDO database connection requires you to create a new PDO object with a Data Source Name (DSN), Username, and Password. The DSN defines the type of database, the name of the database, and any other information related to the database if required. These are the variables and values we stated inside the dbconfig.

How do I connect to SQL Server in Linux?

To connect to a named instance, use the format machinename \ instancename . To connect to a SQL Server Express instance, use the format machinename \SQLEXPRESS. To connect to a SQL Server instance that is not listening on the default port (1433), use the format machinename :port .

How enable Sqlsrv in PHP Linux?

To install a PECL extension for multiple PHP versions, repeat the PECL extension installation for each PHP version. The SQLSRV extension provides functions for accessing Microsoft SQL Server databases.

How to connect a PHP script to MySQL using PDO?

The methods to connect a PHP script to MySQL using PDO is similar to the previous one, but with a slight variation: In the public_html, create a file named pdoconfig.php and insert the following code. As always, don’t forget to replace the placeholder values with your database information.

How do I connect to a SQL Server database using PHP?

Creates a connection resource and opens a connection using the sql_srv driver for PHP. By default, the connection is attempted using Windows Authentication. Download the Microsoft Drivers for PHP for SQL Server to develop PHP applications that connect to SQL Server and Azure SQL Database.

Is the PDO MSSQL driver still available?

ActiveOldestVotes 31 The PDO mssql driver is no more, use sqlsrv(under php windows) or dblib(under php linux) http://msdn.microsoft.com/en-us/sqlserver/ff657782.aspx

What is the difference between mysqli and PDO?

Meanwhile, MySQLi sees data as a set of interchangeable objects with functions, allowing users to add or remove data easily. PDO stands for PHP Data Object. Unlike MySQLi, PDO is only object-oriented and supports a number of different database types that use PHP, such as MySQL, MSSQL, Informix, and PostgreSQL.


2 Answers

The PDO mssql driver is no more, use sqlsrv (under php windows) or dblib (under php linux)

http://msdn.microsoft.com/en-us/sqlserver/ff657782.aspx

http://www.php.net/manual/en/ref.pdo-dblib.php

like image 153
James Avatar answered Oct 11 '22 09:10

James


I am running Ubuntu 14.04. Trying to connect to MSSQL I got "Uncaught exception 'PDOException' with message 'could not find driver'". It seems that I was missing the dblib/sybase PHP extension.

I had to run:

sudo apt-get install php5-sybase freetds-common libsybdb5 \  && sudo apache2ctl restart 

Works fine now.

like image 22
Karl Wilbur Avatar answered Oct 11 '22 07:10

Karl Wilbur