# GoDaddy PHP mssql_query problem



## milledj (Nov 21, 2009)

I am attempting to set up a simple query for my (hosted) GoDaddy database, and I am having trouble getting things to work. I have an example of a working PHP file (I am no expert here, far from it, I am an Oracle DBA), but when I attempted to adapt it to the GoDaddy environment, I am comming up on a brick wall. 

If someone has a working example of a GoDaddy, MSSQL query PHP file that works, that would be great. 
Otherwise, this is what I have working on another server environment, at home: 

```
<?php
$myServer = "habisys.db.2276763.hostedresource.com";
$myUser = "habisys";
$myPass = "****";
$myDB = "habisys"; 
//connection to the database
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
  or die("Couldn't connect to SQL Server on $myServer"); 

//select a database to work with
$selected = mssql_select_db($myDB, $dbhandle)
  or die("Couldn't open database $myDB"); 

$query = "SELECT [PIDM],[BU_Name],[Link] from [habisys].[dbo].[Sponsors]";
$query .= " order by [BU_Name] ";

$result = mssql_query($query);
//execute the SQL query and return records


/* fetching data and output as xml */
print "<cipdata>\n";
while( $Row = mssql_fetch_object( $result ) )
{
print "<program>
 ... data here ... 
</program>\n";
}
print "</cipdata>";
?>
--- now, when I attempted to use it, I would get an error on the server connection. GoDaddy sent me this as proof that the connection is working:
<?php
$myServer = "habisys.db.2276763.hostedresource.com";
$myUser = "habisys";
$myPass = "*****";
$myDB = "habisys"; 

//create an instance of the  ADO connection object
$conn = new COM ("ADODB.Connection")
  or die("Cannot start ADO");

//execute the SQL query and return records
//define connection string, specify database driver
$connStr = "PROVIDER=SQLOLEDB;SERVER=".$myServer.";UID=".$myUser.";PWD=".$myPass.";DATABASE=".$myDB; 
  $conn->open($connStr); //Open the connection to the database
echo "Connection Open";
$conn->Close();
$conn = null;
?>
```
However, if I add the mssql_query to this, I get another connection error on the query. Does the fact that the above example prints out Connection Open when the PHP file is run proof that the connection is indeed open? 
If so, why won't my $query statement run? 

TIA,

DJM


----------



## Redcore (Aug 14, 2007)

Do you get a specific error message?

Also, I'm pretty sure mssql_query requires a connection be specified...


```
$result = mssql_query($query, $dbhandle);
```


----------



## milledj (Nov 21, 2009)

Thanks for the reply. I have tried the mssql_query with and without a link. It seems that if I select the database, I do not have to supply the link. If I use the link in the query, or I select the database I get the following error:

Connection Open
Warning: mssql_query(): supplied argument is not a valid MS SQL-Link resource in D:\Hosting\2276763\html\mssqltest2.php on line 19

Warning: mssql_fetch_object(): supplied argument is not a valid MS SQL-result resource in D:\Hosting\2276763\html\mssqltest2.php on line 26

Line 19: $result=mssql_query($query, $conn); // this is from the ADO conn
Line 26: while( $Row = mssql_fetch_object( $result ) )

Perhaps my connection string is not correct? 

If I add 'or ('error'); ' to the connection, I get 'error' as a result, but again, I don't know PHP so it could be my syntax: 
$conn->open($connStr); or $conn->open($connStr) or ('error');

?

Thanks again for your reply.

DJM


----------



## Redcore (Aug 14, 2007)

I'm not sure what the second code piece is for...are they separate pages or on the same page?



> Line 19: $result=mssql_query($query, $conn); // this is from the ADO conn
> 
> Perhaps my connection string is not correct?
> 
> ...


This looks different than the code you posted. Could you post this code plus the class for "open" ?


----------



## milledj (Nov 21, 2009)

I originally posted 2 separate PHP files, one that works on my own server setup, but not on GoDaddy, the 2nd one is what GoDaddy sent me as proof that a connection to the database works.

The GoDaddy php uses ADO -- this is it again, so it can be clearer:

<?php 
$myServer = "habisys.db.2276763.hostedresource.com"; 
$myUser = "habisys"; 
$myPass = "*****"; 
$myDB = "habisys"; 

//create an instance of the ADO connection object 
$conn = new COM ("ADODB.Connection") 
or die("Cannot start ADO"); 

//execute the SQL query and return records 
//define connection string, specify database driver 
$connStr = "PROVIDER=SQLOLEDB;SERVER=".$myServer.";UID=".$myUser.";PWD=".$myPass.";DATABASE=".$myDB; 
$conn->open($connStr); //Open the connection to the database 
echo "Connection Open"; 
$conn->Close(); 
$conn = null; 
?>


----------



## Redcore (Aug 14, 2007)

So why not use the second DB connection for your scripts on GoDaddy?


----------



## milledj (Nov 21, 2009)

I couldn't use either script because as soon as I attempted to issue any query mssql_query, the PHP script would throw an error. 

I decided to try the same thing but use MYSQL and had it working in about 10 minutes, including setting up the XML. I think that the problem was with GoDaddy and their MS SQL setup. Nothing I tried could successfully issue any commands past the connection and their tech support said that if the connection was working, that was their part and that I was on my own. 
Lesson learned: Don't try MS SQL with GoDaddy...

Thanks for trying to help me out.

DJM


----------



## Redcore (Aug 14, 2007)

Yeah, I'll recommend the same to others. I thought their setup looked a bit on the wonky side.


----------

