URGENT - php mssql error while connecting (iis server)

Hi

I've got a problem while connection to an mssql db server from a php class.

It worked just fine on our test server, also we had no problem using the same db class on other servers.

But now we have moved our code an db to a new host, and all we get is this error:

"Warning: mssql_connect() [function.mssql-connect]: Unable to connect to server: XXX.XXX.XXX.XXX in C:\websites\hmcv.se\subdomains\sok\httpdocs\modules\db\db.class.php on line 14"

I'm 100% sure that what is in the $username, $password, $database and $hostname vars is correct.

We can connect without problem via mssql management studio.

What is diffrent from our test server is that it had an apache server running php,

The host we have uploaded the site to runs IIS with php

Our host tells us it's an scripting error, what i don't know is if you have to write the db class diffrently while running on an IIS server.

Any suggestions will be helpfull.


<?php
class db
{
private $connectlink;	//Database Connection Link
private $username = "#####";
private $password = "#####";
private $database = "hc";
private $hostname = "#####";
private $resultlink;	//Database Result Recordset link
private $rows;		//Stores the rows for the resultset
private $numrows; //Stores number of records returned
 
public function __construct() {
	$this->connectlink = mssql_connect($this->hostname,$this->username,$this->password);
	if(!($this->connectlink)) {
	}
	else {
		mssql_select_db($this->database);
	}
}
public function __destruct() {
	@mssql_close($this->connectlink);
}
public function query($sql) {
	$this->resultlink = mssql_query($sql);
	return $this->resultlink;
}
public function num_rows($queryin) {
	$this->numrows = mssql_num_rows(mssql_query($queryin));
	return $this->numrows;
}
public function fetch_rows($result) {
	$rows = array();
	if($result) {
		while($row = mssql_fetch_assoc($result)) {
			$rows[] = $row;
		}
	}
	else {
		$rows = null;
	}
	return $rows;
}
}
?>

Open in new window

LVL 2
GudorianAsked:
Who is Participating?
 
GudorianConnect With a Mentor Author Commented:
Problem solved

Out hosting company had probably messed up some drivers so when using php with "CGI applications" it didn't work.

However switching to run with "ISAPI extension" instead of CGI fixed the problem.

thx for your help.
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
please try to specify the port in the hostname part:

private $hostname = "ip_address,1433";
0
 
MuralidharanpCommented:
0
Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

 
RovastarCommented:
Does an ODBC/DSN connection work for your login/server details?

TO check the db connectivity?

Can you telnet port 1433 to the SQL server?

If you cannot do this then you likely have network/firewall/routing issues.
0
 
ThiyagarajRamaswamyCommented:
Hello Expert,

Go through the below link for connecting PHP with SQL Server. Hope this might help you.

http://razasayed.wordpress.com/2008/03/09/connecting-to-sql-server-2005-express-using-php/

0
 
GudorianAuthor Commented:
angelll
I've tried to specify port without result, still the same error.
I had no problem transfering the database from our test server to the new host via mssql management studio.

Rovastar
I am able to create an ODB/DNS connection in the PLESK admin system, if that was what you asked?
I've no problem connecting from our network, but it's a remote host that hosts both iis and the mssql server, so I have no permission to connect from the web server to sql via telnet

ThiyagarajRamaswamy and Muralidharanp
Since it's a remote host I can't configure the servers, it's all set up.

0
 
kumaranmcaCommented:
Hi

Please refer the below url.

I think you missed the port number. Kindly check the below url....
http://www.php.net/manual/en/function.mssql-connect.php
0
 
RovastarCommented:
"Rovastar
I am able to create an ODB/DNS connection in the PLESK admin system, if that was what you asked?
I've no problem connecting from our network, but it's a remote host that hosts both iis and the mssql server, so I have no permission to connect from the web server to sql via telnet
"

Can you create an ODBC/DSN connection from the webserver machine to the database server then?

It could be a firewall issue stopping the connectivity. As I do not know your web infrastructuyre it is hard to tell.

I would look at removing that first issue first.
0
 
GudorianAuthor Commented:
Sorry for my late answer,

I'll check whith our web host if the mssql server is running on any diffrent port, since specifying the default port number in the connection doesn't work.

Rovastar

We're using a remote hosting company on a shared server, so we have no full control of the server.

The way to access the server is via PLESK control panel that our host provides.

I've tested creating the dsn but I can't see any problem.


Would a better way be to using a connection string more like the asp way in the script?
If so, how can do I do it?
0
 
GudorianAuthor Commented:
I've been doing some testing with the odbc source and rewritten my db class to fit with the rest of my code.

The code below works just fine, my problem now is the performance.

I've got an ajax enabled search function,

first you choose something in an option box, then the next pop up.

after switching to odbc_connect from mssql_connect it takes forever to load the next results, before on our test server it was no delay at all.

Anyway, I think we could eliminate firewall issues etc. since connection to databse works, but it shouldn't take 10-20secs to load 7 rows of result.

The new code is kinda messy also, i would prefer to have it working by using the mssql_connect function.

All other classes relies on that the data is returned as an vector that looks like
$value[0]['col_name'];
too work

Is it something that can be done about the class posted below or any suggestions of how to get my first db class(se my first post) to work?

<?php
class db
{
private $connectlink;	//Database Connection Link
private $username = "USERNAME";
private $password = "PASSWORD";
private $database = "hc";
private $hostname = "IP";
private $resultlink;	//Database Result Recordset link
private $rows;		//Stores the rows for the resultset
private $numrows; //Stores number of records returned
 
public function __construct() {
	$this->connectlink = odbc_pconnect('hc_con', $this->username, $this->password);//$this->hostname,$this->username,$this->password);
	/*if(!($this->connectlink)) {
	}
	else {
		mssql_select_db($this->database);
	}*/
}
public function __destruct() {
	@odbc_close($this->connectlink);
}
public function query($sql) {
	$this->resultlink = odbc_exec(odbc_pconnect('hc_con', $this->username, $this->password), $sql);
	return $this->resultlink;
}
public function num_rows($queryin) {
	$this->numrows = odbc_num_rows(odbc_exec(odbc_pconnect('hc_con', $this->username, $this->password), $queryin));
	return $this->numrows;
}
public function fetch_rows($result) {
	$rows = array();
	$i=0;
	if($result) {
		$colName = odbc_num_fields($result);
		$r=0;
		while (odbc_fetch_row($result)) 
    	{ 	
    		
    		  for ($j=1; $j<= $colName; $j++)
  		{
			$rows[$r][odbc_field_name($result, $j )] = odbc_result($result, odbc_field_name($result, $j ));
			$i++;
			
		}
		$r++;
    }
	}
	else {
		$rows = null;
	}
	//print_r( $rows);
	return $rows;
}
}
?>

Open in new window

0
All Courses

From novice to tech pro — start learning today.