Php & mssql

Status
Not open for further replies.

ziycon

New Member
[SOLVED]Php & mssql

I've upgraded to PHP 5.2.6 and im running SQL Server Express 2005. Trying to get a simple query from the SQL server to display in a PHP page, i have the MSSQL module showing up in the phpinfo() page and im running the code below, its not givign any errors just keeps printing out the message i have put if for the 'or die' clause.

Any ideas or help is always appreciated.

PHP:
<?
php  = 'sa';
 = 'adm1n';
 = 'ADMINSQLEXPRESS';
 = 'testdb';

 = mssql_connect (,,) or die('Unable to connect to database.');
?>
 

louie

New Member
did you try the server name as "localhost"
 

Solonox

New Member
Hey,
I don't have a lot of experience with PHP these days i am a C# Developer.. However you should be able to do a try / catch with your mssql_connect and then catch the exception. If you could do that and paste the exception here then it will give us much more detail as to what is happening.

Cheers
Dave
 

ziycon

New Member
did you try the server name as "localhost"
Doesn't make any difference.

Hey,
I don't have a lot of experience with PHP these days i am a C# Developer.. However you should be able to do a try / catch with your mssql_connect and then catch the exception. If you could do that and paste the exception here then it will give us much more detail as to what is happening.

Cheers
Dave
Tried this but unable to figure out how to display the error as if it's a SQL error a try catch in the PHP code wont display the error!?!
 

louie

New Member
try this
[code
<?php

$link = mssql_connect ($servername,$dbusername,$dbpassword) or die(mysql_error().'Unable to connect to database.');
?>

[/code]
 

Solonox

New Member
What Louie has given looks to be exactly what you need to write out the exception. Give that a shot and post up the results till we see what's happening.
 

Solonox

New Member
haha... no i don't think so.

Can you try this example

try {
$hostname = "
ADMIN\SQLEXPRESS";
$dbname = "testdb";
$username = "sa";
$pw = "adm1n";

$dbh = new PDO ("mssql:host=$hostname;dbname=$dbname","$username","$pw");
} catch (PDOException $e) {
echo "Connection failed because: " . $e->getMessage() . "\n";
exit;
}


That should write out your exception.. However someone of the PHP guys here may have a better way of doing it.. but that should work ;)
 

ziycon

New Member
Figured it out after a good few hours, its pretty simple!:(

I was using mssql instead of sqlsrv, i'll explain.

Microsoft released a PHP/SQL 2005 driver for interaction so when this driver/extension is installed and instead of using the mssql_connect() you have to use sqlsrv_connect() as there is a whole new API for the MS driver available here API Reference (SQL Server 2005 Driver for PHP), the old way of connecting to SQL via PHP using the ntwdblib.dll file will not work on a SQL installation after SQL Server 7 as its deprecated, so you need to use SQL Server 2005 Driver for PHP Documentation from now on.

Thanks for all your help lads!
 
Status
Not open for further replies.
Top