Connecting to SQL Server byPHP


You can use the following sample script to test connectivity to a Microsoft SQL Server 2012 database through PHP. The script will connect 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
  • myDatabaseName

<?php


$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
$sqlselect = "SELECT * FROM INFORMATION_SCHEMA.TABLES";

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

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

?>



Was this content helpful?




© 2011-2013 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