We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 5340
    • 1,624 Posts
    Hi,

    I am trying to access an external MSSql server from a php file. In the end the information will be brought into a modx document using a snippet.

    The problem is that I can’t connect and I am not sure what should I do next.

    Here is the code I tried so far and the errors I get

    <?php
    
    $myServer = "..........";
    $myUser = "...........";
    $myPass = ".........";
    $myDB = ".......";
    
    $dbhandle = mssql_connect($myServer, $myUser, $myPass)
    or die("Couldn't connect to SQL Server on $myServer");
    
    $selected = mssql_select_db($myDB, $dbhandle)
    or die("Couldn't open database $myDB");
    
    mssql_close($dbhandle);
    
    ?>


    Returns
    Warning: mssql_connect() [function.mssql-connect]: Unable to connect to server: ........... in /var/www/websites/connect-ms.php on line 8
    Couldn’t connect to SQL Server on .....


    <?php
      try {
        $hostname = "......";
        $port = .....;
        $dbname = "....";
        $username = ".......";
        $pw = ".......";
        $dbh = new PDO ("dblib:host=$hostname:$port;dbname=$dbname","$username","$pw");
      } catch (PDOException $e) {
        echo "Failed to get DB handle: " . $e->getMessage() . "\n";
        exit;
      }
      //$stmt = $dbh->prepare("select name from master..sysdatabases where name = db_name()");
      $stmt = $dbh->prepare("show databases");
      $stmt->execute();
      while ($row = $stmt->fetch()) {
        print_r($row);
      }
      unset($dbh); unset($stmt);
    ?>


    Returns
    Failed to get DB handle: could not find driver


    More info on the server

    Configure Command ’./configure’ ’--with-apxs2=/usr/local/apache/bin/apxs’ ’--with-mysql’ ’--with-mssql=/usr/local/freetds’ ’--enable-mbstring’ ’--with-ldap’ ’--with-zlib’ ’--with-gd’ ’--with-jpeg-dir’ ’--with-curl=/usr/include/curl’ ’--enable-shared’ ’--enable-static’ ’--with-freetype-dir=/usr/include/freetype2’ ’--with-openssl’ ’--enable-zip’ ’--with-gd’ ’--with-bz2’ ’--with-mysqli’

    PDO drivers sqlite, sqlite2

    mssql Library version FreeTDS


    In theory the first code should work but it doesn’t. Any ideas?

    Thanks
      • 40429
      • 2 Posts
      try editing your setting at the php.ini
      mssql.secure_connection must be set to on