farp332
farp332

Reputation: 746

How to connect PHP to SQL Server?

I'm trying to connect to SQL Server using PHP, I'm learning this language.

I have read that probably a ODBC connection is the best, I have created mine ODBC but I don't know how to use it from PHP, so far I'm trying this:

$server = 'MyServer\MyDB';

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

if (!$link) {
    die('You cannot connect to MSSQL');
}

When running this I get this message:

OBJECT NOT FOUND, ERROR 404.

But ODBC test is fine.

The SQL Server is located in the same PC, so maybe I don't need to input the IP.

Is there anybody who can help?

Upvotes: 3

Views: 645

Answers (1)

Sampson
Sampson

Reputation: 268344

It appears you may have a typo. Here's an example from the documentation:

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

You mistakenly placed an = after mssql_connect, attempting to make an assignment (as far as the interpreter is concerned).

As for using MSSql with PHP, have a look at the relevant mssql functions. Next you would select your database, and then begin querying it.

Upvotes: 3

Related Questions