Truemag

  • Categories
    • Tips And Tricks
    • Internet
    • PHP
    • Javascript
    • CSharp
    • SQL Server
    • Linux
  • Lastest Videos
  • Our Demos
  • About
  • Contact
  • Home
  • Write With Us
  • Job Request
Home PHP PHP Connect To MS SQL Server

PHP Connect To MS SQL Server

I’m familiar with access and manipulate MS SQL Server in Windows Forms or Asp.net Web Application but today, I have to find a way to connect to MS SQL Server by using PHP.

I’m working on an ASP.NET web application (C#) that connects to MS SQL Server database but there is a function that I spend hours to work out but don’t have any solution yet and I may not meet my deadline. Luckily, it becomes possible if I use PHP to solve this issue and my client just want to get his job done no matter the programming language :). His dedicated server also supports PHP, of course.

However, the PHP function need to connect to MS SQL Server to pull data from some tables. Another issue I have to face with. After research around the Internet, I have a PHP script that connects to MS SQL Server and works properly. I would like to save this script and share with everyone.

Connect to MS SQL Server by using PHP

Below is the PHP script just for demonstration purpose. It connects to my local MS SQL Server, select database, add some dump data and display them.

<?php
$host = "localhost";
$db_user = "sa";
$db_password = "123456";
$database = "[4rapiddev]";
 
//connection to the database
$dbh = mssql_connect($host, $db_user, $db_password) or die("Couldn't connect to SQL Server on $host");
 
//select a database to work with
mssql_select_db($database, $dbh)  or die("Couldn't open database $database");
 
//insert some data for testing purpose
mssql_query("insert into tbl_members(first_name, last_name) values ('Hoan','Huynh')");  
mssql_query("insert into tbl_members(first_name, last_name) values ('Rapid','Development')");  
 
$query = "select * from tbl_members";
 
//execute the SQL query and return records
$result = mssql_query($query);
 
$total_rows = mssql_num_rows($result);
echo "total_rows: " . $total_rows . "<br>";
echo "<hr>";
//display the results
while($row = mssql_fetch_array($result))
{
  echo $row["first_name"] . " " . $row["last_name"] . "<br>";
}
//close the connection
mssql_close($dbh);
?>

<?php $host = "localhost"; $db_user = "sa"; $db_password = "123456"; $database = "[4rapiddev]"; //connection to the database $dbh = mssql_connect($host, $db_user, $db_password) or die("Couldn't connect to SQL Server on $host"); //select a database to work with mssql_select_db($database, $dbh) or die("Couldn't open database $database"); //insert some data for testing purpose mssql_query("insert into tbl_members(first_name, last_name) values ('Hoan','Huynh')"); mssql_query("insert into tbl_members(first_name, last_name) values ('Rapid','Development')"); $query = "select * from tbl_members"; //execute the SQL query and return records $result = mssql_query($query); $total_rows = mssql_num_rows($result); echo "total_rows: " . $total_rows . "<br>"; echo "<hr>"; //display the results while($row = mssql_fetch_array($result)) { echo $row["first_name"] . " " . $row["last_name"] . "<br>"; } //close the connection mssql_close($dbh); ?>

Structure of the tbl_members table above as below:

CREATE TABLE [dbo].[tbl_members](
	[id] [INT] IDENTITY(1,1) NOT NULL,
	[first_name] [nvarchar](50) NULL,
	[last_name] [nvarchar](50) NULL,
	[created_date] [DATE] NULL,
 CONSTRAINT [PK_tbl_members] PRIMARY KEY CLUSTERED 
(
	[id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
 
GO
 
ALTER TABLE [dbo].[tbl_members] ADD  CONSTRAINT [DF_tbl_members_created_date]  DEFAULT (getdate()) FOR [created_date]
GO

CREATE TABLE [dbo].[tbl_members]( [id] [int] IDENTITY(1,1) NOT NULL, [first_name] [nvarchar](50) NULL, [last_name] [nvarchar](50) NULL, [created_date] [date] NULL, CONSTRAINT [PK_tbl_members] PRIMARY KEY CLUSTERED ( [id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO ALTER TABLE [dbo].[tbl_members] ADD CONSTRAINT [DF_tbl_members_created_date] DEFAULT (getdate()) FOR [created_date] GO

Nov 4, 2011Hoan Huynh
Replace String With NText Or Text Data Type In MS SQL ServerLoad And Save Facebook Profile Picture Of User
You Might Also Like:
  • Connect Remotely To MySQL or SQL Server Databases for Godaddy Hosting Account
  • Fix Can Not Connect To Proxy Server In Firefox, Google Chrome And Internet Explorer
  • PHP Connect To MySQL Read Data And Export XML Format
  • IE Enable/Disable Proxy Server by using script file
  • Save Table Structure Changes When Remote Access To SQL Server
  • How To Check Which Stored Procedures Or SQL Commands Are Running On SQL Server
  • How To Track Website With Multiple Google Analytisc Accounts
  • Facebook Like Button And Recommend Button With fb:like, iframe and html5
  • C# Get List of databases in SQL Server
  • Get Image Width Height With JQuery And JavaScript
Hoan Huynh

Hoan Huynh is the founder and head of 4rapiddev.com. Reach him at [email protected]

9 years ago PHPmssql_connect, mssql_query, mssql_select_db154
0
GooglePlus
0
Facebook
0
Twitter
0
Digg
0
Delicious
0
Stumbleupon
0
Linkedin
0
Pinterest
Most Viewed
PHP Download Image Or File From URL
22,485 views
Notepad Plus Plus Compare Plugin
How To Install Compare Text Plugin In Notepad Plus Plus
20,284 views
Microsoft SQL Server 2008 Attach Remove Log
Delete, Shrink, Eliminate Transaction Log .LDF File
16,043 views
JQuery Allow only numeric characters or only alphabet characters in textbox
13,519 views
C# Read Json From URL And Parse/Deserialize Json
10,067 views
4 Rapid Development is a central page that is targeted at newbie and professional programmers, database administrators, system admin, web masters and bloggers.
Recent Posts
  • Online Payday Loans – Learn How To Make the Most of A Alternative Lending Option
  • Strategies For Buying Photo Editor Software
  • Where to Find the Greatest Free Photo Editor on the Web
  • Custom Research Paper – What’s it So Useful?

  • Getting Bad Credit Paydayloans From a Reputable Source
Categories
  • CSharp (45)
  • Facebook Graph API (19)
  • Google API (7)
  • Internet (87)
  • iPhone XCode (8)
  • Javascript (35)
  • Linux (27)
  • MySQL (16)
  • PHP (84)
  • Problem Issue Error (29)
  • Resources (32)
  • SQL Server (25)
  • Timeline (5)
  • Tips And Tricks (141)
  • Uncategorized (247)
Recommended
  • Custom Software Development Company
  • Online Useful Tools
  • Premium Themes
  • VPS
2014 © 4 Rapid Development