Search code examples
phpsql-servermacosfreetds

Can't connect mssql with PHP under mac Yosemite


I'm using PHP 5.5 under Mac Yosemite, the default php with this SO. i'm trying to connect to MSSQL DB server but it's imposible with a lot of alternatives.

I tried to install freetds and the command works but when i tried with PHP...its look he is trying to load but the connection close. My code on PHP is like this:

$server = 'XXX.XXX.XXX.XXX' ;
$user = "username";
$pass = "password";
$DB = "";

$link = mssql_connect($server, $user, $pass) ;

if(!$link){
    die('Something goes wrong');
}

I look into php info and it's enabled: php info

¿Someone knows what is the best alternative to connect to mssql db and works?


Solution

  • Right now it's working with these lines:

    try {
        $hostname = 'XXX.XXX.XXX.XXX';
        $port = 1433;
        $dbname = "YOUR_DB";
        $username = "YOUR_USERNAME";
        $pw = "YOUR_PASS";
        $dbh = new PDO ("dblib:host=$hostname:$port;dbname=$dbname","$username","$pw");
    } catch (PDOException $e) {
        echo "Failed to get DB handle: " . $e->getMessage() . "\n";
        exit;
    }
    

    First you have to install PDO_DBLIB in your system.