简体   繁体   中英

Connect to MS SQL Server with PHP without drivers

A project at work has come up that requires me to connect to a Microsoft SQL Server 2014 in PHP. I have found tons of solutions online that involve downloading a set of drivers to either use PDO, sqlsrv, or ODBC.

After contacting my hosting provider this is what they said:

As you're on shared hosting, drivers can't be installed as it's not supported on our platform.

You can use normal ODBC to connect if required

I so far haven't been able to find a solution that works without having to install any drivers/extensions. The hosting provider recommended ODBC but I can't seem to get my code to work for ODBC either. Does anyone know any other way I would be able to do this?

The code I'm currently using (which gives me an error:

Can't open lib 'SQL Server'

$server = 'xxx.xxx.xxx.xxx';
$user = 'user.....';
$pass = 'password...';

$port='1433';
$database = 'database...';

$connection_string = "DRIVER={SQL Server};SERVER=$server;PORT=$port;DATABASE=$database";
$conn = odbc_connect($connection_string,$user,$pass);
if ($conn) {
    echo "Connection established.";
} else{
    die("Connection could not be established.");
}

Have you tried this?

<?php
$serverName = "serverName\\sqlexpress, 1542"; //serverName\instanceName, portNumber (default is 1433)
$connectionInfo = array( "Database"=>"dbName", "UID"=>"userName", "PWD"=>"password");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

if( $conn ) {
     echo "Connection established.<br />";
}else{
     echo "Connection could not be established.<br />";
     die( print_r( sqlsrv_errors(), true));
}
?>

Refer: https://www.php.net/manual/en/function.sqlsrv-connect.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM