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

PHP with MS SQL

Hello All,

How can we connect PHP with MS - SQL?

Thanks beforehand.
0
Mohit Vijay
Asked:
Mohit Vijay
  • 9
  • 4
  • 4
  • +1
11 Solutions
 
jet-blackCommented:
Hi,

If you have Windows based hosting, it will be simple because mssql module is already installed on Windows based hosting.
However it is very hard to find Unix based hosting that the mssql module is installed. However I can suggest one company which install this module if you contact and ask them to install it:
www.pacifichost.com


Connecting to mssql database is very similar to connecting to mysql databases.
Here is the all functions of mssql module:
http://php.net/manual/en/book.mssql.php





<?php
define('DATABASE_HOST_NAME', 'ip_address'); //if your database port is different than the default one, you should replace ip_address with ip_address:port
define('DATABASE_USER_NAME', 'sa');
define('DATABASE_USER_PASSWORD', 'password');

$connection = mssql_connect(DATABASE_HOST_NAME, DATABASE_USER_NAME, DATABASE_USER_PASSWORD);

if(!$connection)
{
    die('could not connected');
}
else
{
    echo "connected";
}


//test
$sql_Recordset1 = "SELECT * FROM database";
$recordset1 = mssql_query($sql_Recordset1);

while ($row_Recordset1 = mssql_fetch_assoc($recordset1))
{
    echo $row_Recordset1['id'];
}
?>

Open in new window

0
 
Mohit VijayAuthor Commented:
Yes We are using Windows based hosting. Actually we are using our own server.

Its Windows Server 2008
Installed IIS
using PHP on IIS
SQL Express 2008 available
We are using Moodle.

any specific to any of these?
0
 
jet-blackCommented:
check php configuration <?php phpinfo(); ?> and confirm that the mssql module is listed there.
it should run without any problems.

0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
Mohit VijayAuthor Commented:
We are using FreeTDS version 8.0 and added its extension.
0
 
jet-blackCommented:
ok, so everything seems to be ok.
0
 
Mohit VijayAuthor Commented:
No, I am unable to connect with server. always connection failed message is appearing :(, something is missing.
0
 
jet-blackCommented:
Did you tried to connect using the instance name?


<?php
// Server in the this format: <computer>\<instance name> or 
// <server>,<port> when using a non default port number
$server = 'KALLESPC\SQLEXPRESS';

// Connect to MSSQL
$link = mssql_connect($server, 'sa', 'phpfi');

if (!$link) {
    die('Something went wrong while connecting to MSSQL');
}
?>

Open in new window

0
 
Mohit VijayAuthor Commented:
Question, Is it required to use sa account?
0
 
Mohit VijayAuthor Commented:
I created a small testing app. in vb.net,

when I use

Server=WEBSRV01\SQLEXPRESS;Database=Devdb;User ID=Test;Password=Hello123;Trusted_Connection=False;

connect worked fine with vb.net application.

and when I tried to use

Server=WEBSRV01\SQLEXPRESS,1433;Network Library=DBMSSOCN;Initial Catalog=devdb;User ID=Test;Password=Hello123;

get a error, Login failed for user "Test"

0
 
p_nutsCommented:
the connection string is different

mssql_connect('server','user',password');

mssql_select_db('dbname);

0
 
p_nutsCommented:
0
 
Ray PaseurCommented:
This function returns FALSE on error:
http://www.php.net/manual/en/function.mssql-connect.php

In MySQL we have some error handling functions.  Maybe there is some analog for those in MSSQL?
http://www.php.net/manual/en/function.mssql-get-last-message.php

You might find some diagnostic information in that function.
0
 
Mohit VijayAuthor Commented:
Thanks Ray_Paseur,

I was thinking about such error information. Let me try it today, will let you know if I need something else. :)

BTW I am using connection string as

mssql_connect('server','user',password') in Test.php
0
 
Ray PaseurCommented:
From the way I read the online manual, that looks correct.  Will be interesting to see if you're getting FALSE from the connect command.
0
 
Mohit VijayAuthor Commented:
How can I know either I am using thread Safe or non-thread safe php version?
0
 
Mohit VijayAuthor Commented:
I got false while connect, so its mean there is some error while connecting. but problem is I am not getting error information, like what type of error occurred :(
0
 
Ray PaseurCommented:
Please post the code that failed, thanks.  You have verified the passwords (case-sensitive), right?
0
 
Mohit VijayAuthor Commented:
yes, password, username, servername everything has been checked several times..

failed code is similar to

mssql_connect('server','user',password')
0
 
Ray PaseurCommented:
That does not tell us much, since we cannot see the other code, or how the return is tested.  If you want to post the code, please use the code snippet and post enough of the code that we can see what is really going on.  Also, please have a look at the user notes on this page:
http://php.net/manual/en/function.mssql-connect.php
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 9
  • 4
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now