Connecting to SQL Server through PHP

You can use the following sample script to test connectivity to a Microsoft SQL Server 2012 database through PHP. The script connects to the database and produces a list of the tables contained within it.

Note: To use the sample script, you must replace the following placeholders with your database details:

  • myDatabaseUsername
  • myDatabasePassword
  • myDatabaseHostname (without the instance name)
  • myDatabaseName

$username = "myDatabaseUsername";
$password = "myDatabasePassword";
$hostname = 'myDatabaseHostname';
$dbname = "myDatabaseName";

//connection to the database
$dbcon = mssql_connect($hostname, $username, $password)or die("Unable to connect to MSSQL");
echo "Connected to MSSQL";

//select the database
mssql_select_db($dbname, $dbcon);

//SQL Select statement

//Run the SQL query
$sqlquery = mssql_query($sqlselect);

//Output the query results
while ($result = mssql_fetch_array($sqlquery) )
echo "<br>";
echo "<br>";



© 2015 Rackspace US, Inc.

Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License

See license specifics and DISCLAIMER