1

I have a php file with a mssql connection, which works on windows.

Now I would like to run my php on linux.

I installed unixodbc and msodbcsql.

The server is running RedhatEnterprise 6 and php 5.4

I cant find any description how to connect to a mssql database with linux.

What else do I have to install and configure?

3 Answers 3

2

You can certainly connect directly to Microsoft SQL Server database from a Linux server, using PHP's PDO system and the dblib driver.

First, install the EPEL repository and then install the php-mssql package. Then use the following code to set up your connection:

$dbhost = "your.db.server";
$dbname = "yourdbname";
$dbuser = "yourdbuser";
$dbpass = "yourdbpassword";

$db = new PDO("dblib:$dbhost;dbname=$dbname", $dbuser, $dbpass);

foreach ($db->query("select * from mytable") as $row) {
    echo "{$row['field1]'}<br/>";
}

Additional StackOverflow resources on this topic include:

Connecting to mssql using pdo through php and linux

pdo dblib on centos 6.x

Sign up to request clarification or add additional context in comments.

Comments

0

I faced the same challenge very recently, and after a lot of head scratching ultimately, I decided to go about things a little differently, as follows.

I set up a Windows machine (it could be a virtual machine) running PHP and Apache to handle the MSSQL requests from the Linux box.

The PHP on Linux uses a CURL command to send a request to Windows, and the response contains the data (compressed).

A little off-beat perhaps, but working well.

Comments

0

You need to have the modules sqlsrv and pdo_sqlsrv active.

sudo apt-get install php-fpm pcp-cli php-dev php-pear
sudo pecl install sqlsrv pdo_sqlsrv

The correct location (on Linux) to put these two lines is in /etc/php/<version>/mods-available/pdo.ini there, add

extension=sqlsrv.so
extension=pdo_sqlsrv.so

Then restart/reload php-fpm or the computer.

Then run ./test.php (where port 2017 is the SQL-server-port)

#!/usr/bin/php
<?php
$serverName = "localhost,2017";
$connectionOptions = array(
    "database" => "MY_DB_NAME",
    "uid" => "sa",
    "pwd" => "TOP_SECRET"
);

// Establishes the connection
$conn = sqlsrv_connect($serverName, $connectionOptions);
if ($conn === false) {
    die(formatErrors(sqlsrv_errors()));
}

// Select Query
$tsql = "SELECT @@Version AS SQL_VERSION";

// Executes the query
$stmt = sqlsrv_query($conn, $tsql);

// Error handling
if ($stmt === false) {
    die(formatErrors(sqlsrv_errors()));
}
?>

<h1> Results : </h1>

<?php
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
    echo $row['SQL_VERSION'] . PHP_EOL;
}

sqlsrv_free_stmt($stmt);
sqlsrv_close($conn);

function formatErrors($errors)
{
    // Display errors
    echo "Error information: <br/>";
    foreach ($errors as $error) {
        echo "SQLSTATE: ". $error['SQLSTATE'] . "<br/>";
        echo "Code: ". $error['code'] . "<br/>";
        echo "Message: ". $error['message'] . "<br/>";
    }
}
?>

And to query the db with SQL-server pdo:

$servername = "";
$username = "";
$password = "";
$database = "";
$port = "1433";
try 
{
    $dbh = new PDO("sqlsrv:server=$servername,$port;Database=$database;ConnectionPooling=0", $username, $password,
        array(
            PDO::ATTR_PERSISTENT => true,
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
        )
    );
} 
catch (PDOException $e) 
{
    echo ("Error connecting to SQL Server: " . $e->getMessage());
}



 $stmt = $dbh->prepare("SELECT name FROM master.sys.databases WHERE name = db_name()");
  $stmt->execute();
  while ($row = $stmt->fetch()) {
    print_r($row);
  }
  unset($dbh); unset($stmt);

2 Comments

Since your examples are using the vendor-specific functions prefixed sqlsrv_, you shouldn't need to install pdo_sqlsrv as well.
@IMSoP: That's the ms-sample. Added PDO.

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.