Simple MS-SQL example

This is a simple example on how to query a Microsoft SQL Server by using PHP.

Snippet information

Author:
Jonas John

License:
Public Domain

Language:
PHP

Created:
09/18/2007

Updated:
09/18/2007

Tags:
,

Related links:
- Microsoft SQL Server Functions


/*
** 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/>\n\n";
 
// Fetch rows:
while ($Row = mssql_fetch_assoc($result)) {
 
    print $Row['Fieldname'] . "\n";
 
}
 
mssql_close($con);


Found a bug? Or do you have a better solution for this?
Feel free to leave a message:

Add a comment


Leave a comment

Luke Hardiman June 15, 2010 at 04:07
$result = mssql_query($SQL)
or die('A error occured: ' . mysql_error());

.mysql_error is ment to be mssql_get_last_message