954,604 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

php with mssql connection error

hi all,

this is first time connect php with mssql 2005

code :

<?php
 
$myServer = "srv\ins";
$myUser = "";
$myPass = "";
$myDB = "db1"; 
 
//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"); 
 ?>


but return error "mssql_connect(): Unable to connect to server"

i think something missing in my code when you have instance, because same code run successfully without instance

i hope to help me because iam seraching this problem and not found any solution

مصعب علي
Newbie Poster
5 posts since Jun 2010
Reputation Points: 10
Solved Threads: 1
 

What is srv\ins ? It should be a hostname or ip address.

pritaeas
Posting Expert
Moderator
5,484 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

Check this function for more detail.
mysql_connect

vibhaJ
Posting Shark
931 posts since Apr 2010
Reputation Points: 161
Solved Threads: 183
 
Check this function for more detail. mysql_connect

I think you mean this one: mssql_connect

pritaeas
Posting Expert
Moderator
5,484 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

A shot in the dark. Sorry I can't test this; I don't have a named instance.
Try escaping the backslash: "server\\instance"
Please let us know how it turns out so some of us can hang on to the remaining few hairs we have.

d.jordan
Newbie Poster
5 posts since Mar 2010
Reputation Points: 10
Solved Threads: 1
 

Its most probally your mssql setup. I have had horrible issues with mssql and php.

Does your sql server reside on the same computer as your web server?

Point your server var at the ip address of the server.

I prefer to use odbc connections when forced to develope with mssql.
http://uk2.php.net/manual/en/function.odbc-connect.php

omol
Junior Poster
156 posts since Jul 2004
Reputation Points: 10
Solved Threads: 10
 
rajarajan07
Nearly a Posting Virtuoso
1,447 posts since May 2008
Reputation Points: 167
Solved Threads: 239
 

srv = server name
ins = instance name

apache server installed in same machine of database, not remote connection i don't need remote connection, i work under test environment without instance my code is run but in real environment there was instance and the code return error that i mention it

if there some extension should be enabled or some configuration missing

mr.rajarajan07 i read your post but no new still same error, anyhow thank you but if you have another solution do not hesitate to tell me

مصعب علي
Newbie Poster
5 posts since Jun 2010
Reputation Points: 10
Solved Threads: 1
 

Have you tried using odbc connections instead?

omol
Junior Poster
156 posts since Jul 2004
Reputation Points: 10
Solved Threads: 10
 

iam not tried because i already have 231 php page contain mssql_query() function that should be connection by mssql_connect() function , this code i wrote it run in all company branches successfully but one branch have instance for sql server that made problem

مصعب علي
Newbie Poster
5 posts since Jun 2010
Reputation Points: 10
Solved Threads: 1
 

hi

you first refer the function properly

here i write function which help you to connect OK

you use this code ok

it work on my connection i will check it on my PC then i send you

//code started
<?php

$conn=mysql_connect("localhost","root");

if($conn)
{
echo "connected";
}
?>

here local host is"default server name"
and root is "defaultusername"

swi2
Newbie Poster
19 posts since Jul 2010
Reputation Points: 10
Solved Threads: 3
 

Please tellme its working or not plz

swi2
Newbie Poster
19 posts since Jul 2010
Reputation Points: 10
Solved Threads: 3
 

sorry but i mean mssql_connect() not mysql_connect()
my DBMS is sql server 2005 not mysql

and server with instance ,if you have any idea in this issue please send me your suggestion

مصعب علي
Newbie Poster
5 posts since Jun 2010
Reputation Points: 10
Solved Threads: 1
 

hi, According to me you use mysql in place of mssql and I'll modify your error code with correct code on basis of mysql,

<?php
//Generally we try to run php script at localhost.
$myServer = "localhost";
$myUser = "root";
$myPass = " ";
$myDB = "db1";
//connection to the database
$dbhandle = mysql_connect($myServer, $myUser, $myPass);
if (!$dbhandle)
  {
  die('Could not connect: ' . mysql_error());
  }
//select a database to work with
$selected = mysql_select_db($myDB, $dbhandle)
if (!$selected)
  {
  die('Data Base not Found:' . mysql_error());
  }

/*
Write here yours Further code. 
*/



?>


and if you have problem in that code, please mention here I'll try to solve them

sandeepji1
Newbie Poster
17 posts since Jun 2010
Reputation Points: 10
Solved Threads: 1
 

This is getting awkward. Sandeepji1, he has told you he is using MSSQL not MySQL. The two are completly diffrent, the only thing they share is the name SQL.

I will give you hand as people seem to be messing you around so much.

First lets check to see if the sql module is loaded.

if(function_exists('mssql_connect')){
     echo "MSSQL Module loaded";
} else {
     echo "MSSQL Module not loaded";
}


Add this to a php file and let me know what the results are.

omol
Junior Poster
156 posts since Jul 2004
Reputation Points: 10
Solved Threads: 10
 

see this example it very simple code to connection

<?php
mysql_connect('localhost','root','') or die('Not connected to server');

mysql_select_db('db_name') or die('Not selected DB');

?>
manzarr
Light Poster
39 posts since Jul 2010
Reputation Points: 11
Solved Threads: 1
 
iam not tried because i already have 231 php page contain mssql_query() function that should be connection by mssql_connect() function , this code i wrote it run in all company branches successfully but one branch have instance for sql server that made problem

Perhaps the user you are trying to connect with does not have sufficient rights. Seeing that only branch has problems with it, it does not appear to be a coding error. More likely a configuration or rights issue on SQLServer or some firewall misbehaving.

Can you change your die('Could not ...') to die(mssql_error()) and output what it says here. It should provide some additional information.

pritaeas
Posting Expert
Moderator
5,484 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You