0

I'm trying to connect SQL Server 2005 using PHP. I searched in Google but am not getting proper solution, it's showing search results about mssql_connect() is not working properly. By seeing this am not getting anything.

      require_once 'login.php';
     $db_server = mysql_connect($db_hostname, $db_username, $db_password);
     mysql_select_db($db_database) or die("Unable to connect to database: ".mysql_error());

This will connect mysql database. I tried replace mysql_connect() with mssql_connect(). But its not working. login.php has

  $db_hostname = 'localhost';
  $db_database = 'urlstore';
  $db_username = 'root';
  $db_password = 'tiger';

How can I connect SQL database using PHP.

2 Answers 2

1

You need to install MS SQL PHP extensions and then you can work with your MS SQL Server the way you are used to.

Here is the information about the extension and how to install it.

Sign up to request clarification or add additional context in comments.

Comments

0

You could use PDO if your server configuration supports it. PDO is an abstraction layer that allows you to connect to many different database types using the same object.

<?php
try {
    $dbh = new PDO ('mssql:host='.$mssql_server.';dbname='.$mssql_db, $mssql_login, $mssql_pwd);
    $dbh->exec("INSERT INTO tablename(column1, column2) VALUES ('stuff', 'here')");
    $dbh = null;
    }
catch (PDOException $e)
    {
    echo $e->getMessage();
    }
?>

This code can help you determine if PDO is enabled:

<?php
    foreach(PDO::getAvailableDrivers() as $driver)
    {
        echo $driver.'<br />';
    }
?>

OR you can run this:

<?php phpInfo(); ?>

and look for this:

PDO

PDO support PDO drivers

enabled dblib, mysql, odbc, pgsql, sqlite, sqlite2

If it isn't enabled for MSSQL, you can uncomment the "extension=php_pdo_mssql.dll" line in php.ini.

If it still doesn't work, you might want to try this:

ntwdblib.dll - The most common issue is that you do not have the ntwdblib.dll file installed in your PHP directory (where php.exe is, or sometimes placing it in the ext directory works as well). This library can be found with your Enterprise Manager dll's or in your SQL servers system32 folder. It's generally best to take the file from the server where SQL Server is installed -quoted from http://www.helpspot.com/helpdesk/index.php?pg=kb.page&id=13

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.