Web Hosting Forum - A Web Hosting community for Webmasters


Divi WordPress Theme

Author Topic: Sample PHP MSSQL connection script  (Read 31392 times)

0 Members and 1 Guest are viewing this topic.

Offline Kailash

  • Administrator
  • Web Hosting Master
  • *****
  • Posts: 601
  • iTrader: +153/-0
    • View Profile
Sample PHP MSSQL connection script
« on: December 05, 2008, 10:16:16 PM »
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:

Code: [Select]
<?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.
« Last Edit: February 08, 2017, 09:02:39 AM by Kailash »

Divi WordPress Theme
Tags:
 

Related Topics

  Subject / Started by Replies Last post
0 Replies
2546 Views
Last post August 30, 2007, 02:04:54 AM
by Tech
1 Replies
3511 Views
Last post December 22, 2011, 05:52:05 PM
by wilsondavid
0 Replies
5939 Views
Last post November 30, 2008, 10:47:40 AM
by Kailash
0 Replies
610 Views
Last post October 14, 2014, 12:42:58 PM
by zeropid
0 Replies
486 Views
Last post December 14, 2014, 07:53:50 PM
by wRick