phpsql-servervxml

Basic data retrieval using $row


I am using PHP 5.6 and SQL Server 2012 with a nonthreadsafe driver. I have an incredibly easy question I already know, but for some reason i am completely brain dead.

I have a basic SQL query that will can either return 1 row of data, or multiple.

There is only three fields with data. but I would like to be able to read off the data (this is going into a vXML ivr using Voice Server 4.0, but that is irrelevant to my question).

Here is my code:

<?php
$serverName = "localhost";
$connectionOptions = array("Database"=>"mydb");
/* Connect using Windows Authentication. */
$conn = sqlsrv_connect( $serverName, $connectionOptions);
if( $conn ) {
     echo "Connection established.";
}else{
     echo "Connection could not be established.";
     die( print_r( sqlsrv_errors(), true));
}

$sql = "SELECT * FROM my_table WHERE SSN = 111111111";

//////I have no idea if I need this or not input appreciated
$stmt = sqlsrv_query( $conn, $sql );
if( $stmt === false ) {
     die( print_r( sqlsrv_errors(), true));
}
?>

All I would like to do is be able to retreive the information as rows, like

   echo  $row['field'];
   echo $row['field2'];
   echo $row['field3'];
    ect.

would someone be able to point me in the right direction? I can only find mysqli examples, and those do not seem to work. Thanks!


Solution

  • I assume what you're looking for is sqlsrv_fetch_array

    sqlsrv_fetch_array — Returns a row as an array

    An Example:

    $stmt = sqlsrv_query( $conn, $sql );
    if( $stmt === false ) {
         die( print_r( sqlsrv_errors(), true));
    }
    while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ) {
          echo $row['field'].", ".$row['field2']."<br />";
    }
    

    PHP Manual: sqlsrv_fetch_array