PHP - Jobba med databaser

Webbutveckling / PHP

Läs från MSSQL-databas

Spar exempelkoden nedan i en fil med filändelsen .php

Exempelkod:

<?php

$serverName = "PCNAME\SQLEXPRESS"; //serverName\instanceName

// Since UID and PWD are not specified in the $connectionInfo array,
// The connection will be attempted using Windows Authentication.

$connectionInfo = array("Database" => "db_name");
$conn = sqlsrv_connect($serverName, $connectionInfo);

if($conn){
echo "Connected to database";
} else {
echo "Connection to database could not be established";
die(print_r(sqlsrv_errors(), true));
}

$query = "SELECT * FROM users";
$result = sqlsrv_query($conn, $query);

// while there are rows to be fetched...
while($list = sqlsrv_fetch_object($result)) {
// echo data
echo $list -> FirstName.", ".$list -> LastName."<br>";
} 

sqlsrv_close($conn);
?>