• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 811
  • Last Modified:

Connecting to MS SQL with PHP

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.
APD Toronto
APD Toronto
4 Solutions
Dave BaldwinFixer of ProblemsCommented:
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.
APD TorontoAuthor Commented:
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?
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:
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

@fighter - that code won't work without the SQL driver.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now