Simple MS-SQL example in PHP

This is a simple example on how to query a Microsoft SQL Server by using PHP.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/*
** Connect to database:
*/
 
// Connect to the database (host, username, password)
$con = mssql_connect('localhost','admin','foo') 
    or die('Could not connect to the server!');
 
// Select a database:
mssql_select_db('Northwind') 
    or die('Could not select a database.');
 
// Example query: (TOP 10 equal LIMIT 0,10 in MySQL)
$SQL = "SELECT TOP 10 * FROM ExampleTable ORDER BY ID ASC";
 
// Execute query:
$result = mssql_query($SQL) 
    or die('A error occured: ' . mysql_error());
 
// Get result count:
$Count = mssql_num_rows($result);
print "Showing $count rows:<hr/>nn";
 
// Fetch rows:
while ($Row = mssql_fetch_assoc($result)) {
 
    print $Row['Fieldname'] . "n";
 
}
 
mssql_close($con);
X

Url: http://www.jonasjohn.de/snippets/php/mssql-example.htm

Language: PHP | User: ShareMySnippets | Created: Jan 12, 2014 | Tags: mssql example