Functions for accessing other databases, including Informix and Sybase, can be found in the PHP manual. For DBMSs that are not supported natively by PHP, ODBC can usually be used; we discuss ODBC later in this section.
Microsoft SQL Server
Similarly to the MySQL function library, there are many functions for connecting to, querying, and extracting results from Microsoft SQL Server DBMSs.
SQL Server can be used under the Microsoft Windows operating system by making minor changes to THE configuration of PHP in the php.ini file; these changes are discussed in the online PHP manual. SQL Server can also be accessed from a Linux platform by installing the FreeTDS package available from http://www.freetds.org
and recompiling PHP with the -with-sybase option; this enables both Sybase and SQL Server support. SQL Server databases can also be accessed using the ODBC library discussed in the next section.
Six functions are listed here, and Example 4-12 shows these implemented in a modified version of Example 4-1.
- resource mssql_connect(string
host
, stringusername
, stringpassword
) -
Establishes a connection to a SQL Server DBMS. On success, the function returns a connection resource handle that can access databases through subsequent commands. Returns
false
on failure.The parameters (all of which are optional) and their use are identical to those of the
mysql_connect( )
function. - int mssql_select_db(string
database
, resourceconnection
) -
Uses the
database
on theconnection
, where theconnection
is a resource returned frommssql_connect( )
. - resource mssql_query(string
SQL_command
, resourceconnection
) -
Runs an SQL command through the
connection
created withmssql_connect( )
on the database selected withmssql_select_db( )
. Returns a resource-a result handle used to fetch the result set-on success andfalse
on failure. - array mssql_fetch_row(resource
result_set
) -
Fetches the result set data, row-by-row, following an
mssql_query( )
command using theresult_set
resource returned by the query. The results are returned as an array, and use is again identical tomysql_fetch_row( )
.false
is returned when no more rows are available. - int mssql_num_fields(resource
result_set
) -
Returns the number of attributes in a
result_set
resource handle, where theresult_set
handle is returned frommssql_query( )
. - int mssql_close(resource
connection
) -
Closes a SQL Server connection opened with
mssql_connect( )
.
Example 4-12. Connecting to a Microsoft SQL Server database with PHP
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Wines</title> </head> <body><pre> <?php <? // (1) Open the database connection and select the // winestore $connection = mssql_connect("localhost","fred","shhh"); mssql_select_db("winestore", $connection); // (2) Run the query on the winestore through the // connection $result = mssql_query("SELECT * FROM wine", $connection); // (3) While there are still rows in the result set while ($row = mssql_fetch_row($result)) { // (4) Print out each attribute in the row for ($i=0; $i<mssql_num_fields($result); $i++) echo $row[$i] . " "; // Print a carriage return to neaten the output echo "\n"; } // (5) Close the database connection mssql_close($connection); ?> </pre> </body> </html>