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

Recommended Answers

All 16 Replies

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

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.

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

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

Have you tried using odbc connections instead?

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

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"

Please tellme its working or not plz

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

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

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.

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');

?>

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.