<?php
$serverName = "(local)\sqlexpress";
$connectionOptions = array( "Database"=>"AdventureWorks" );
/* Connect to SQL Server using Windows Authentication. */
$conn = sqlsrv_connect( $serverName, $connectionOptions );
/* Get products by querying against the product name.*/
$tsql = "SELECT ProductID, Name, Color, Size, ListPrice FROM Production.Product";
/* Execute the query. */
$getProducts = sqlsrv_query( $conn, $tsql );
/* Loop thru recordset and display each record. */
while( $row = sqlsrv_fetch_array( $getProducts, SQLSRV_FETCH_ASSOC ) )
{
print_r( $row );
}
/* Free the statement and connection resource. */
sqlsrv_free_stmt( $getProducts );
sqlsrv_close( $conn );
?>
גישה לבסיסי הנתונים בגישת PDO (עם רכיב SQLSRV_PDO):
<?php
$serverName = "(local)\sqlexpress";
/* Connect to SQL Server using Windows Authentication. */
$conn = new PDO( sqlsrv:server=$serverName;Database=AdventureWorks" );
/* Get products by querying against the product name.*/
$tsql = "SELECT ProductID, Name, Color, Size, ListPrice FROM Production.Product";
/* Execute the query. */
$getProducts = $conn->query( $tsql );
/* Loop thru recordset and display each record. */
while( $row = $getProducts->fetch( PDO::FETCH_ASSOC ) )
{
print_r( $row );
}
/* Free the statement and connection resource. */
$getProducts = NULL;
$conn = NULL;
?>
תהנו!