How to Get the SQL Azure Session Tracing ID using PHP

The SQL Azure team recently posted a blog about SQL Azure and the Session Tracing ID. The short story about the Session Tracing ID is that it is a new property (a unique GUID) for connections to SQL Azure. The nice thing about it is that if you have a SQL Azure error, you can contact Azure Developer Support and they can use it to look-up the error and help figure out what caused it. (If you are just getting started with PHP and SQL Azure, see this post: Getting Started with PHP and SQL Azure.)

Getting the Session Tracing ID is easy with PHP…just execute the following query: SELECT CONVERT(NVARCHAR(36), CONTEXT_INFO()). Here’s the PHP code for doing this:

$server = ",1433";
$user = "YourUserName@YourServerID";
$pass = "YourPassword";
$database = "DatabaseName";
$connectionoptions = array("Database" => $database, "UID" => $user, "PWD" => $pass);
$conn = sqlsrv_connect($server, $connectionoptions);

if($conn === false)
    die(print_r(sqlsrv_errors(), true));

$stmt = sqlsrv_query($conn, $sql);
$row = sqlsrv_fetch_array($stmt);

Of course, the code above assumes you have the SQL Server Driver for PHP installed. And, if you are watching closely, you’ll notice that I didn’t have to include the “MultipleActiveResultSets”=> false in my connection options array…because SQL Azure now supports Multiple Active Result Sets (MARS).

That’s it for today…hope this can come in handy.



Share this on Twitter

Skip to main content