Connecting to MS SQL with PHP

Posted on 2012-08-17
Last Modified: 2012-08-20
Hello Experts,

I’m a novice when it comes to PHP, but do have lots of Classic ASP experience.

I’m wondering does anyone  know if it is possible to connect to a MS SQL (not mySQL) DB from GoDaddy’s 2GH Shared Linux Plan?  I know that I do not have the option to setup am MS SQL DB on this hosting plan, but what if I have an external SQL Server elsewhere, could I connect to it from this hosting in PHP by just specifying the IP Address, User Name, Password and Database Name?

Assuming that I am able to connect, can anyone help me to write a test script, along the lines of:
strConn = "Driver={SQL Server}; " & _
	  "Server=123.456.78.987; " & _
	  "Database=someDB; " & _
	  "Uid=SOMEone; " & _

strSQL = "SELECT * FROM Table"

rs.Open strSQL, strConn

Table contents: <br><br>

Do While Not rs.EOF

	Response.Write(rs.Fields("fld1") & "<br>")


Open in new window

…but, of  course, in PHP?

Many Thanks.
Question by:APD_Toronto
    LVL 82

    Assisted Solution

    by:Dave Baldwin
    I don't believe you will be able to do that from the Linux server or from Godaddy hosting.  The MS SQL drivers do not run on Linux and last time I checked, Godaddy will not let you connect to an external MS SQL (or MySQL) database server.  Others have asked about this and had to change to different hosting to do it.

    Author Comment

    i have actually connected from GoDddy's Shared Win to external SQL.  However, i worry if their Linux have the req'd drivers.

    Can anyone help me convert the above code?
    LVL 34

    Assisted Solution

    It's not a matter of converting code.

    On Linux, the drivers are different and require a separate process for installing. They rely on unixODBC and FreeTDS, with a freetds.conf that has a predefined host name in it.  I kid you not, I set this up on my own server this morning, and I've done it a few times before, so I know what's required. It's not going to happen on GoDaddy's Linux shared hosting.

    This is a guide that reflects the steps that you usually have to take:

    Assisted Solution

    Hello, it is really problem, to connect mssql. I use this code, to connect from unix server, or windows server to mssql.
    try{//try via odbc
    			$connection = conf('remoteDB');
    			$dsn = "dblib:host=".$connection['host'].";dbname=".$connection['db'];
        		$this->link=new PDO("odbc:Driver={SQL Server};Server=".$connection['host'].",".$connection['port'].";Database=".$connection['db']."; Uid=".$connection['login'].";Pwd=".$connection['pass'].";");
    		catch (PDOException $e){
    			$failMess = "Connect error: ".$e->getCode()."<br>". $e->getMessage();
         	    }//end za catch
            if (!$this->work){
            	printDebug("-##-connection je: ",$connection);
            	if (!function_exists('mssql_pconnect') or !function_exists('mssql_query')){
            		$this->work = false;
            		regFailM("Mssql_pconnect not avail try via PDO");
            		return false;
    		    if ($this->link){
    		    	$this->mode = 'normal';
    			    printDebug("pripojeno pres mssql_connect");
    				mssql_select_db($connection['db'], $this->link);
    				$version = mssql_query('SELECT @@VERSION');
    				$row = mssql_fetch_array($version);
    	    		echo $row[0];
    	    		return $this->link;
    		    }else{//not connect
    		    	printDebug("Definitivne NEpripojeno");}
    		    	regFailM($failMess." mssql_connect fail too");
            	$this->mode = 'object';
            	return $this->link;
                }//endif za $this->work

    Open in new window

    LVL 34

    Accepted Solution

    @fighter - that code won't work without the SQL driver.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    The purpose of this article is to demonstrate how we can use conditional statements using Python.
    It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now