Page 1 of 1

How do I convert a mssql_field_name function to sqlsrv

Posted: Fri Nov 11, 2016 2:59 am
by pavithra_infog
Hi,

I am converting the mssql functions in my application to sqlsrv functions as part of a migration activity from SQL Server 2003 to SQL Server 2008. I am not getting the expected results when I just replace mssql_field_name with its equivalent in sqlsrv i.e sqlsrv_field_metadata. I am not sure if I am using the right sqlsrv function for this. Could anyone help me here. The code which I am trying to convert looks something like this:

Code: Select all

<?php
$result = mssql_query($query, $conn);
$rows  = mssql_num_rows($result);
$cols = mssql_num_fields($result);

if(rows>0)
{
for($i= 0;$i<$cols;++$i)
{
$colname = mssql_field_name($result,$i);
echo " <th>$colname</th>";
}
echo "</tr>";
}

if($rows<=0){
echo "No Results found for the specified inputs";
exit();
}else
{
    for($i = 0;$i < $rows; $i++)
   {
echo "<tr>";
for($j =0;$j < $cols; $j++)
{
$colname = mssql_field_name($result,$j);
$out = mssql_result($result,$i,"$colname");
echo "<td> $out &nbsp;</td>";
}
echo "</tr>";
    }

echo "<br>";
}
?>

Re: How do I convert a mssql_field_name function to sqlsrv

Posted: Fri Nov 11, 2016 10:31 am
by Christopher
sqlsrv_field_metadata($stmt) returns an array of arrays. There is an array of information for each field. I think you can do something similar to what you are doing like this:

Code: Select all

foreach (sqlsrv_field_metadata($stmt) as $colname => $metadata) {
    echo " <th>$colname or {$metadata['Name']}</th>";
}
See: https://msdn.microsoft.com/en-us/library/cc296197.aspx

Re: How do I convert a mssql_field_name function to sqlsrv

Posted: Sun Nov 13, 2016 10:49 pm
by pavithra_infog
Hi Christopher,

Thanks for providing the syntax of equivalent sqlsrv function for mssql_field_name. I am now able to get the expected results after changing the code snippet.


Best Regards,
Pavithra