Simple MS-SQL example
This is a simple example on how to query a Microsoft SQL Server by using PHP.
/* ** 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);
Snippet Details
-
AuthorJonas John
-
LicensePublic Domain
-
LanguagePHP
-
Created09/18/2007
-
Updated09/18/2007
-
Tagsdatabase functions, example
- Related links
Sorry folks, comments have been deactivated for now due to the large amount of spam.
Please try to post your questions or problems on a related programming board, a suitable mailing list, a programming chat-room,
or use a QA website like stackoverflow because I'm usually too busy to answer any mails related
to my code snippets. Therefore please just mail me if you found a serious bug... Thank you!
Older comments:
Adam Kinney June 22, 2011 at 18:34
What if your database column name has a space in it or a special character? ( for example: Entry Date or complete? ) This is an issue I am running into and it won’t display the data.
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
or die('A error occured: ' . mysql_error());
.mysql_error is ment to be mssql_get_last_message