• Welcome to Web Hosting Community Forum for Webmasters - Web hosting Forum.
 

Recommended Providers

Fully Managed WordPress Hosting
lc_banner_leadgen_3
Fully Managed WordPress Hosting

WordPress Theme

Divi WordPress Theme
WPZOOM

Forum Membership

Forum Membership

Sample PHP MSSQL connection script

Started by Kailash, December 05, 2008, 10:16:16 PM

Kailash

As of PHP 5.3, default MSSQL driver is no longer available in PHP. As an alternative, you can use PDO_SQLSRV Driver to connect to your MSSQL database. Before you use this extension, you will have to enable it from your PHP configuration. You can refer the following URL for sample code:

http://msdn.microsoft.com/en-us/library/ff754357%28v=sql.105%29.aspx


For native php_msql diver, the following script should work:

<?php
$Server 
"localhost";
$User "your_name";
$Pass "your_password";
$DB "examples";

//connection to the database
$dbconn mssql_connect($Server$User$Pass)
  or die(
"Couldn't connect to SQL Server on $Server");

//select a database to work with
$selected mssql_select_db($DB$dbconn)
  or die(
"Couldn't open database $myDB");

//declare the SQL statement that will query the database
$query "SELECT name from test ";

//execute the SQL query and return records
$result mssql_query($query);

$numRows mssql_num_rows($result);
echo 
"<h1>" $numRows " Row" . ($numRows == "" "s") . " Returned </h1>";

//display the results
while($row mssql_fetch_array($result))
{
  echo 
"<br>" $row["name"];
}
//close the connection
mssql_close($dbconn);
?>


If you are using PHP MSSQL driver, you can refer Sample PHP MSSQL connection script using PHP MSSQL driver.